JS函数-比较2个对象数组(每个数组中都有一个数组对象)

时间:2018-07-29 21:54:57

标签: javascript jquery arrays query-performance

我有一个用户列表以及他们已经收到的内容。

users = [
    { name: 'Steve', received_contents: ['1a', '1b', '3c'] },
    { name: 'Virginie', received_contents: ['3a', '2b', '3c'] },
    { name: 'Fiona', received_contents: ['1b', '2a', '3b'] },
    { name: 'Jenny', received_contents: ['3b', '2c', '1b'] },
    { name: 'James', received_contents: ['2b', '1b', '3a'] },
    { name: 'Fede', received_contents: ['2c', '3a', '1c'] },
    { name: 'Sara', received_contents: ['3a', '2c', '3b'] },
    { name: 'Tizi', received_contents: ['2b', '1b', '2a'] },
    { name: 'Thomas', received_contents: ['3c', '2b', '1a'] },
]

//这些是下次发货的箱子及其内容

boxes = [
{ code: 'gr1', contents: ['1a', '1b'] },
{ code: 'gr2', contents: ['1a', '2b'] },
{ code: 'gr3', contents: ['1b', '2c'] },
{ code: 'gr4', contents: ['2c', '3c'] },
{ code: 'gr5', contents: ['3b', '1c'] },
]

任务是创建一个函数,该函数接受用户列表并返回用户列表以及可以接收的框,而无需再次获得相同的内容。

我对如何使我的解决方案更有效,更省时有些困惑。

这是我的解决方法:

    for (var i in users ){
        let user = users[i];
        console.log("User "+user.name+ " can receive " + getReceivableBoxes(user.received_contents));
    }

    function getReceivableBoxes (contents){
        let receivableBoxes = [];
        for(var i in boxes){
            let box = boxes[i];
            let canReceive = canReceiveBox(contents, box);
            if(canReceive){
                receivableBoxes.push(box.code);

            }

        }

        return receivableBoxes;
    }

    function canReceiveBox(received_contents, box) {
        let receivableBoxes = [];

        for (var i = 0; i < received_contents.length; i++) {
            for (var j = 0; j < box.contents.length; j++) {
                if (box.contents[j] === received_contents[i]) {
                    return false;
                }
            }
        }
        return true;
    }

2 个答案:

答案 0 :(得分:0)

性能

JS

function getReceivableBoxes(user){
  const receivableBoxes = [];

  for(let b in boxes){
    const box = boxes[b];
    const contents = box.contents;
    const filtered = [];

    for(var i = 0, k = contents.length; i < k; i++){
      if(user.received_contents.indexOf(contents[i]) > -1){
        break;
      }

      filtered.push(contents[i]);
    }

    if(filtered.length === contents.length){
      receivableBoxes.push(box.code);
    }
  }

  return receivableBoxes;
}

紧凑版本

JS

users.forEach(function(user){
  var receivableBoxes = [];

  boxes.forEach(function(box){ 
    var contents = box.contents.filter(function(item){ 
      return !user.received_contents.includes(item);
    });

    if(contents.length === box.contents.length){
      receivableBoxes.push(box.code);
    }
  });

  console.log('@@@ User ' + user.name + ' can receive ' + receivableBoxes.join(','));
});

ES6

const users = [
    { name: 'Steve', received_contents: ['1a', '1b', '3c'] },
    { name: 'Virginie', received_contents: ['3a', '2b', '3c'] },
    { name: 'Fiona', received_contents: ['1b', '2a', '3b'] },
    { name: 'Jenny', received_contents: ['3b', '2c', '1b'] },
    { name: 'James', received_contents: ['2b', '1b', '3a'] },
    { name: 'Fede', received_contents: ['2c', '3a', '1c'] },
    { name: 'Sara', received_contents: ['3a', '2c', '3b'] },
    { name: 'Tizi', received_contents: ['2b', '1b', '2a'] },
    { name: 'Thomas', received_contents: ['3c', '2b', '1a'] }
]

const boxes = [
  { code: 'gr1', contents: ['1a', '1b'] },
  { code: 'gr2', contents: ['1a', '2b'] },
  { code: 'gr3', contents: ['1b', '2c'] },
  { code: 'gr4', contents: ['2c', '3c'] },
  { code: 'gr5', contents: ['3b', '1c'] }
];

users.forEach((user) => {
  const receivableBoxes = [];

  boxes.forEach((box) => { 
    const contents = box.contents.filter((item) => !user.received_contents.includes(item));

    if(contents.length === box.contents.length){
      receivableBoxes.push(box.code);
    }
  });
      
  console.log(`@@@ User ${user.name} can receive ${receivableBoxes.join(',')}`);
});

DEMO

答案 1 :(得分:0)

您可以使用ES6数组函数,映射,过滤,查找以及每一个。这些功能已由现代JavaScript引擎的开发人员高度优化。定时它通常在0或1毫秒内执行,与上面的代码运行所需的时间相同。

我还想指出,如果您在针对现代JavaScript采访的技术测试中达到了for循环或forEach的要求,那将给您带来打击。

const users = [
    { name: 'Steve', received_contents: ['1a', '1b', '3c'] },
    { name: 'Virginie', received_contents: ['3a', '2b', '3c'] },
    { name: 'Fiona', received_contents: ['1b', '2a', '3b'] },
    { name: 'Jenny', received_contents: ['3b', '2c', '1b'] },
    { name: 'James', received_contents: ['2b', '1b', '3a'] },
    { name: 'Fede', received_contents: ['2c', '3a', '1c'] },
    { name: 'Sara', received_contents: ['3a', '2c', '3b'] },
    { name: 'Tizi', received_contents: ['2b', '1b', '2a'] },
    { name: 'Thomas', received_contents: ['3c', '2b', '1a'] },
];
// These are the boxes for the next shipment and their contents

const boxes = [
{ code: 'gr1', contents: ['1a', '1b'] },
{ code: 'gr2', contents: ['1a', '2b'] },
{ code: 'gr3', contents: ['1b', '2c'] },
{ code: 'gr4', contents: ['2c', '3c'] },
{ code: 'gr5', contents: ['3b', '1c'] },
];

const start = new Date();
const usersWithBoxes = users.map(user => ({
  user: user,
  boxes: boxes.filter(box => box.contents.every(bc => !user.received_contents.find(rc => bc === rc)))
}));
const end = new Date();

console.log(`Function took ${end.getTime() - start.getTime()}ms to run`);
console.log(usersWithBoxes.map(u => `${u.user.name} can recieve boxes ${u.boxes.map(b => b.code)}`).join('\n'));