Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
80 views
in Technique[技术] by (71.8m points)

javascript - How does one compare specific key-values within a nested object / array structure?

i have two arrays as follows

array1 = {name:"John",surname:"doe",reference:[{name:"jane",surname:"smith",array:[{name:"test",surname:"test",position:12}]},{name:"kate",surname:"post",array:[{name:"test",surname:"test",position:12}]}],id:"12364",weight:"50",Oid:"456978",code:"12"}

array2 =  {name:"John",surname:"doe",reference:[{name:"jane",surname:"smith,array:[{name:"test",surname:"test",position:12}]"},{name:"kate",surname:"post",array:[{name:"test",surname:"test",position:12}]}],id:"4589632",weight:50,policy:"745896",result:"test",documents:"no",launch:"no"}

as you can see both of my lengths of arrays dont match so i can not compare them both.

I would like to compare only certain objects from array 1 and array 2. for example only the objects that i require

array1
name:"John"
surname:"doe"
reference:[{name:"jane",surname:"smith",array:[{name:"test",surname:"test",position:12}]},{name:"kate",surname:"post",array:[{name:"test",surname:"test",position:12}]}]
weight:"50"

array 2
name:"John"
surname:"doe"
reference:[{name:"jane",surname:"smith",array:[{name:"test",surname:"test",position:12}]},{name:"kate",surname:"post",array:[{name:"test",surname:"test",position:12}]}]
weight:50

i only want to compare the above objects key values so the above would return true

if i had

 array1
    name:"John"
    surname:"doe"
    reference:[{name:"jane",surname:"smith",array:[{name:"test",surname:"test",position:12}]},{name:"kate",surname:"post",array:[{name:"test",surname:"test",position:12}]}]
    weight:"12"

array 2
name:"John"
surname:"Petter"
reference:[{name:"jane",surname:"smith",array:[{name:"test",surname:"t2",position:12}]},{name:"kate",surname:"Knight",array:[{name:"test",surname:"test",position:14}]}]
weight:50

the above would be false.

how can i achieve this? how can i filter/loop through them to only compare the above mentioned values.

question from:https://stackoverflow.com/questions/65915340/how-does-one-compare-specific-key-values-within-a-nested-object-array-structur

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can create a function that will do the check.


Notes :

  • We use == for comparaison because in your example weight: 50 is to be equals with weight: '50' (string and number).
  • The function also compare the inner array reference using the same keys to compare.

function compareObjects(obj1, obj2, keysToCompare) {
  return keysToCompare.every(x => obj1[x] instanceof Array ?
    obj1[x].every((y, yi) => compareObjects(y, obj2[x][yi], Object.keys(y))) :
    obj1[x] == obj2[x]);
}

// Nothing is different - should work
console.log(compareObjects({
  name: 'John',
  surname: 'doe',
  reference: [{
      name: 'jane',
      surname: 'smith',
    },
    {
      name: 'kate',
      surname: 'post'
    },
  ],
  id: '12364',
  weight: '50',
  Oid: '456978',
  code: '12',
}, {
  name: 'John',
  surname: 'doe',
  reference: [{
    name: 'jane',
    surname: 'smith',
  }, {
    name: 'kate',
    surname: 'post',
  }],
  id: '4589632',
  weight: 50,
  policy: '745896',
  result: 'test',
  documents: 'no',
  launch: 'no',
}, [
  'name',
  'surname',
  'reference',
  'weight',
]));

// One key is different - should fail
console.log(compareObjects({
  name: 'John',
  surname: 'doe',
  reference: [{
    name: 'jane',
    surname: 'smith',
  }, {
    name: 'kate',
    surname: 'post',
  }],
  id: '12364',
  weight: '50',
  Oid: '456978',
  code: '12',
}, {
  name: 'John',
  // This value is different
  surname: 'FOOL',
  reference: [{
    name: 'jane',
    surname: 'smith',
  }, {
    name: 'kate',
    surname: 'post',
  }],
  id: '4589632',
  weight: 50,
  policy: '745896',
  result: 'test',
  documents: 'no',
  launch: 'no',
}, [
  'name',
  'surname',
  'reference',
  'weight',
]));

// One key is different - should fail
console.log(compareObjects({
  name: 'John',
  surname: 'doe',
  reference: [{
    name: 'jane',
    surname: 'smith',
  }, {
    name: 'kate',
    surname: 'post',
  }],
  id: '12364',
  weight: '50',
  Oid: '456978',
  code: '12',
}, {
  name: 'John',
  surname: 'doe',
  reference: [{
    name: 'jane',
    // This value is different
    surname: 'FOOL',
  }, {
    name: 'kate',
    surname: 'post',
  }],
  id: '4589632',
  weight: 50,
  policy: '745896',
  result: 'test',
  documents: 'no',
  launch: 'no',
}, [
  'name',
  'surname',
  'reference',
  'weight',
]));

// Position is different - should work
console.log(compareObjects({
  name: 'John',
  surname: 'doe',
  reference: [{
      name: 'jane',
      surname: 'smith',
      position: 10,
    },
    {
      name: 'kate',
      surname: 'post',
      position: 10,
    },
  ],
  id: '12364',
  weight: '50',
  Oid: '456978',
  code: '12',
}, {
  name: 'John',
  surname: 'doe',
  reference: [{
    name: 'jane',
    surname: 'smith',
    position: 10,
  }, {
    name: 'kate',
    surname: 'post',
    position: 15,
  }],
  id: '4589632',
  weight: 50,
  policy: '745896',
  result: 'test',
  documents: 'no',
  launch: 'no',
}, [
  'name',
  'surname',
  'reference',
  'weight',
]));

// Everything is same - should work (including position)
console.log(compareObjects({
  name: 'John',
  surname: 'doe',
  reference: [{
      name: 'jane',
      surname: 'smith',
      position: 10,
    },
    {
      name: 'kate',
      surname: 'post',
      position: 10,
    },
  ],
  id: '12364',
  weight: '50',
  Oid: '456978',
  code: '12',
}, {
  name: 'John',
  surname: 'doe',
  reference: [{
    name: 'jane',
    surname: 'smith',
    position: 10,
  }, {
    name: 'kate',
    surname: 'post',
    position: 10,
  }],
  id: '4589632',
  weight: 50,
  policy: '745896',
  result: 'test',
  documents: 'no',
  launch: 'no',
}, [
  'name',
  'surname',
  'reference',
  'weight',
]));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...