-1

as you can see i have 2 arrays

var obj1 = [
    "planet-1",
    "planet-2",
    "planet-3",
    "planet-4"
]

and

var obj2 = [
    "planet-1",
    "planet-8"
]

i was find something like console.log(obj1 == obj2) will return true using stringify, that is almost close from i need but the problem is my json data not have exactly the same between obj1 and obj2, so when i use code like console.log(obj1) == obj2) the return will always false because of json data not have exactly the same.

so can someone please to help me how to figure it out how to make the result will return true if my "obj2" have atleast 1 or 2 data that the same like "obj1" even i have 1 array that not available in obj1?

im sorry i cant show any code for reference, cause i just find console.log(obj1) == obj2) who really close to what im lookin for.

thanks for helping.

mb-1208
  • 1
  • 2
  • You'll have to write your own code that makes the comparison according to what your application needs. – Pointy Mar 28 '22 at 12:33
  • 3
    "_as you can see i have 2 difference json_" - No, those are two arrays. Nothing to do with JSON. – Ivar Mar 28 '22 at 12:36

1 Answers1

0

try this

let difference = obj1.filter(x => !obj2.includes(x));
console.log(difference); // ["planet-2", "planet-3"]

//or

console.log(difference.length > 0); //true

or if you don't know which array is bigger, you can use this code

const isDifferent = function (arr1, arr2) {
  let difference = false;
  if (arr1.length > arr2.length)
    difference = arr1.filter((x) => !arr2.includes(x));
  else difference = arr2.filter((x) => !arr1.includes(x));
  return difference.length > 0;
};

console.log(isDifferent(obj2,obj1)); // true
Serge
  • 28,094
  • 4
  • 11
  • 35
  • is it possible if i add value to obj2 that is not available in obj1 then return true? is there any way? – mb-1208 Mar 28 '22 at 12:51
  • @mb-1208 If you have another questions, it's usually better to ask another question try and not muddy the question, otherwise it's less useful to other users. – Keith Mar 28 '22 at 12:53
  • @mb-1208 You have to test it. if you use a function, it doesn't matter which array is first and which is second. You can put them in any order. For the first case obj1 should be larger then obj2. Let me know if you have any problelm. – Serge Mar 28 '22 at 12:54
  • @Serge return is always true even i randomly change obj2 – mb-1208 Mar 28 '22 at 13:05
  • @mb-1208 The name of the function is `isDifferent` meaning it will return true if different.. You will then get `false` if the same. – Keith Mar 28 '22 at 13:07
  • @mb-1208 , it means isDifferent true. Create 2 the same arrays, and you will get false. – Serge Mar 28 '22 at 13:10
  • You can simplify your fn by just returning true if the lengths are different, and then a single filter will be fine. – Keith Mar 28 '22 at 13:15
  • If I would be doing javascript library I would add a lot of validations, but I think it is out of the scope of this question. And about the length, there is nothing about if array elements can be repeating, so for example ["planet-1", "planet-1", "planet-3", "planet-4"]; and ["planet-1", "planet-3", "planet-4"]; will return false – Serge Mar 28 '22 at 13:25