Название немного многословно, но ниже я создал довольно хороший пример, который подчеркивает, к чему я стремлюсь:
// the array that is being JOINED onto the the main array
var playerArray = [
{ id: 'tom', num: 34, age: 12 },
{ id: 'joe', num: 24, age: 14 },
{ id: 'bim', num: 14, age: 15 },
{ id: 'tim', num: 43, age: 16 },
{ id: 'nik', num: 10, age: 17 },
{ id: 'jib', num: 12, age: 87 }
];
// the main array
var dataArray = [
{ name: 'tom', pts: 24, team: 'bozos', city: 'detroit' },
{ name: 'joe', pts: 14, team: 'bozos', city: 'chicago' },
{ name: 'bim', pts: 34, team: 'kazos', city: 'milkway' },
{ name: 'tim', pts: 51, team: 'kazos', city: 'dragzon' }
];
// loop each row in dataArray, find players number in playerArray, add to dataArray
let thisNum;
dataArray.forEach((currVal, idx, thisArray) => {
thisNum = playerArray
.filter(thisP => thisP.id === currVal.name)
.map(thisP => thisP.num)[0];
thisArray[idx].num = thisNum;
});
console.log('dataArray: ', dataArray);
0: {name: "tom", pts: 24, team: "bozos", city: "detroit", num: 34}
1: {name: "joe", pts: 14, team: "bozos", city: "chicago", num: 24}
2: {name: "bim", pts: 34, team: "kazos", city: "milkway", num: 14}
3: {name: "tim", pts: 51, team: "kazos", city: "dragzon", num: 43}
length: 4
__proto__: Array(0)
Так что моя цель здесь - добавить правильные num
из playerArray
на dataArray
, и приведенный выше пример успешен в этом.Однако фактический dataArray
в моем проекте имеет ~ 10000 объектов, а фактический playerArray
имеет ~ 2000 объектов, и я не думаю, что выполнение 10000 фильтров объекта длиной 2000 является особенно эффективным.
Просто любопытно, есть ли лучший способ сделать это в моем приложении React?Вышесказанное кажется не особенно эффективным.