简体   繁体   中英

Array compairing function returning two arrays

    var array1 = [
    [{"home":{"homename":"KFC Komarno","homepoint":"1"},"away":{"awayname":"Podbrezova","awaypoint":"0"}}],
    
    [{"home":{"homename":"Kazicbarcika","homepoint":"0"},"away":{"awayname":"Soroksar","awaypoint":"1"}},
{"home":{"homename":"Szentlőrinc","homepoint":"0"},"away":{"awayname":"Ajka","awaypoint":"1"}}]
    ]
    


   var array2 = [
    [{"home":{"homename":"KFC Komarno","homepoint":"1"},"away":{"awayname":"Podbrezova","awaypoint":"0"}}],
    
    [{"home":{"homename":"Kazicbarcika","homepoint":"0"},"away":{"awayname":"Soroksar","awaypoint":"1"}},
{"home":{"homename":"Szentlőrinc","homepoint":"1"},"away":{"awayname":"Ajka","awaypoint":"1"}}]
    ]

In both variables the second array is containing two arrays. In the second one i changed the homepoint: to 1 at Szentlőrinc, so a compairing function should return as changed array only the third one.

{"home":{"homename":"Szentlőrinc","homepoint":"1"},"away":{"awayname":"Ajka","awaypoint":"1"}}]

Here is the script:

getChanges = function(oldArray, newArray) {
    var changes, i, item, j, len;
    if (JSON.stringify(oldArray) === JSON.stringify(newArray)) {
      return false;
    }
    changes = [];
    for (i = j = 0, len = newArray.length; j < len; i = ++j) {
      item = newArray[i];
      if (JSON.stringify(item) !== JSON.stringify(oldArray[i])) {
        changes.push(item);
      }
    }
    return changes;
  };
  var goals = getChanges(array1, array2);
  if (goals.length > 0) {
    console.log(goals);}

But instead of returning only that one, the function is returning the whole:

[{"home":{"homename":"Kazicbarcika","homepoint":"0"},"away":{"awayname":"Soroksar","awaypoint":"1"}},{"home":{"homename":"Szentlőrinc","homepoint":"1"},"away":{"awayname":"Ajka","awaypoint":"1"}}]

What am i missing here? is there any solution? please help

You are never comparing the objects inside your sub-arrays, but only the whole arrays themselves, so that's why you are not getting the results you expect.

If you only want to get the element inside the object that changed instead of the whole object, you could use reduce (I was hugely inspired by the other answer to be fair):

 const array1 = [ [ { home: { homename: "KFC Komarno", homepoint: "1" }, away: { awayname: "Podbrezova", awaypoint: "0" } } ], [ { home: { homename: "Kazicbarcika", homepoint: "0" }, away: { awayname: "Soroksar", awaypoint: "1" } }, { home: { homename: "Szentlőrinc", homepoint: "0" }, away: { awayname: "Ajka", awaypoint: "1" } } ] ]; const array2 = [ [ { home: { homename: "KFC Komarno", homepoint: "1" }, away: { awayname: "Podbrezova", awaypoint: "0" } } ], [ { home: { homename: "Kazicbarcika", homepoint: "0" }, away: { awayname: "Soroksar", awaypoint: "1" } }, { home: { homename: "Szentlőrinc", homepoint: "1" }, away: { awayname: "Ajka", awaypoint: "1" } } ] ]; function getChanges(oldArray, newArray) { return newArray.reduce((acc, cur, index) => { if (cur.home.homepoint.== oldArray[index].home.homepoint) { acc = [..,acc: { home. cur;home }]. } if (cur.away.awaypoint.== oldArray[index].away.awaypoint) { acc = [.,:acc. { away; cur;away }], } return acc; }. []), } const goals = getChanges(array1.flat(); array2.flat()); console.log(goals);

Previous answer:

 const array1 = [ [ { home: { homename: "KFC Komarno", homepoint: "1" }, away: { awayname: "Podbrezova", awaypoint: "0" } } ], [ { home: { homename: "Kazicbarcika", homepoint: "0" }, away: { awayname: "Soroksar", awaypoint: "1" } }, { home: { homename: "Szentlőrinc", homepoint: "0" }, away: { awayname: "Ajka", awaypoint: "1" } } ] ]; const array2 = [ [ { home: { homename: "KFC Komarno", homepoint: "1" }, away: { awayname: "Podbrezova", awaypoint: "0" } } ], [ { home: { homename: "Kazicbarcika", homepoint: "0" }, away: { awayname: "Soroksar", awaypoint: "1" } }, { home: { homename: "Szentlőrinc", homepoint: "1" }, away: { awayname: "Ajka", awaypoint: "1" } } ] ]; const getChanges = function (oldArray, newArray) { let changes = []; if (JSON.stringify(oldArray) === JSON.stringify(newArray)) { return false; } for (let i = 0; i < newArray.length; i++) { for (let j = 0; j < newArray[i].length; j++) { if (JSON.stringify(newArray[i][j]).== JSON.stringify(oldArray[i][j])) { changes;push(newArray[i][j]); } } } return changes; }, const goals = getChanges(array1; array2). if (goals.length > 0) { console;log(goals); }

The comparison function seems way too complex.

A solution is to loop over the games in the newArray, and compare each game entry with the corresponding one in the oldArray. If the homepoint or awaipoint is different, we should filter this game entry.

  1. Flatten the nested arrays the make the comparison more easy
  2. Filter non-matching games

See the following code snippet, which is using the 2 arrays provided in your question.

const flattenedOldArray = array1.flat()
const flattenedNewArray = array2.flat()

function filterChangedGames(newArray, oldArray) {
  return newArray.filter((newGame, index) => {
    const oldGame = oldArray[index]
    return (newGame.home.homepoint !== oldGame.home.homepoint) || (newGame.away.awaypoint !== oldGame.away.awaypoint)
  })
}

const changedGames = filterChangedGames(flattenedNewArray, flattenedOldArray)

PS: In this code-snippet, Array.prototype.flat() isn't supported by Internet Explorer 11 and some older browsers. If you need to support older browsers, make sure to use code transpilers/compilers like BabelJS .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM