在对象数组中查找数据数组

时间:2016-05-26 09:38:58

标签: javascript arrays javascript-objects

我有一个如下所示的值数组:

["Ashar", 68345, 14, 46100, "Retail", 1, ""]

我还有一个对象数组,如下所示:

[
 {id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]}, 
 {id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]}, 
 {id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]}, 
 {id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]}, 
 {id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}
]

我要做的是通过匹配两个数组从第二个数组中获取对象中的id。我编写了一个find函数,理论上应该在第二个数组中找到值数组,以便我可以访问id。但是,它返回undefined,所以我正在做的事情显然是错误的。

以下是我目前的代码:

let row_id = rows.find(row => {
    dataEntriesArray === row.cells;
});

其中rows是第二个数组,dataEntriesArray是第一个数组。

非常感谢任何帮助我解决这个问题。

感谢您的时间。

4 个答案:

答案 0 :(得分:1)

使用Array#every



var ip1 = ["Ashar", 68345, 14, 46100, "Retail", 1, ""];

var ip2 = [{
  id: 1,
  cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]
}, {
  id: 2,
  cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]
}, {
  id: 3,
  cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]
}, {
  id: 4,
  cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]
}, {
  id: 5,
  cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]
}];
var op = ip2.filter(function(item) {
  return item.cells.every(function(el) {
    return ip1.indexOf(el) > -1;
  })
});
console.log(JSON.stringify(op));




答案 1 :(得分:1)

此提案使用cells数组检查search数组的每个部分,并返回id(如果找到)。



function getId(data, search) {
    var id;
    data.some(a => {
        if (a.cells.every((b, i) => b === search[i])) {
            id = a.id;
            return true;
        }
    });
    return id;
}

var search =["Ashar", 68345, 14, 46100, "Retail", 1, ""],
    data=    [{id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]}, {id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]}, {id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]}, {id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]}, {id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}];

console.log(getId(data, search));




答案 2 :(得分:1)

试试这个:

var search =["Ashar", 68345, 14, 46100, "Retail", 1, ""],
foundId = null,
a = [{id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]}, {id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]}, {id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]}, {id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]}, {id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}];
search = search.sort().join();
a.forEach(function(entry) {
 if (entry.cells.sort().join() === search){
     foundId = entry.id;
   return;
 }
});

alert(foundId)

JS Fiddle

答案 3 :(得分:1)

使用Array.someJSON.stringify函数的简短解决方案:

var item = ["Rob", 94448, 17, 11395, "Retail", 1, ""],
    data = [{id: 1, cells: ["Ashar", 68345, 14, 46100, "Retail", 1, ""]}, {id: 2, cells: ["Ashar", 300881, 14, 37000, "Retail", 3, 3]}, {id: 3, cells: ["Rob", 94448, 17, 11395, "Retail", 1, ""]}, {id: 4, cells: ["Shahab", 19023870, 219, 12500, "Retail", 1, ""]}, {id: 5, cells: ["David", 29008000, 229, 12500, "Retail", 5, 26]}];

var id;
data.some((obj) => JSON.stringify(obj['cells']) === JSON.stringify(item) && (id = obj['id']), id);

console.log(id);  // 3