我基本上是在尝试从一个对象数组中获取另一个对象的所有数据。例如:
first_array = [{id: 1, name:'something'},{id: 2, name:'something 2'},{id: 3, name:'something 3'},{id: 4, name:'something 4'}]
现在这个数组可以有数千个对象。
下一个阵列是
second_array = [{idx: 40, name:'something',matchingId:1 },{idx: 30, name:'something 2'},{idx: 60, name:'something 3',matchingId:1 },{idx: 20, name:'something 4',matchingId:2},{idx: 25, name:'something 4',matchingId:2},{idx: 26, name:'something 4',matchingId:3}]
现在使用这个secondarray中的matchingId,我想从firstarray中获取元素,然后返回所有对象。现在,使用新创建的数组,我想找到第三个数组中具有相同id的所有元素。
third_array = [{id: 1, name:'something',matchingIdFromSecond:40 },{id: 2, name:'something 2'},{id: 3, name:'something 3',matchingIdFromSecond:40 },{id: 3, name:'something 4',matchingIdFromSecond:30},{id: 4, name:'something 4',matchingIdFromSecond:2},{id: 5, name:'something 4',matchingIdFromSecond:26}]
因此,总的来说,我试图用3个表实现一个连接,但使用vaniallajs代码。
我尝试使用过滤器和将结果映射到third_array,但它不起作用。
这就是我正在努力实现的整体流程
first_array -> use id
second_array -> first_array_ids be matched with second_array.matchingId
third_array -> data returned from second_array use id, and then find all the elements inside third_array.
希望我能把自己说清楚。
您只需映射第一个数组,然后从第二个数组中找到相应的索引,如下所示:
const first_array = [{id: 1, name:'something'},{id: 2, name:'something 2'},{id: 3, name:'something 3'},{id: 4, name:'something 4'}];
const second_array = [{idx: 40, name:'something',matchingId:1 },{idx: 30, name:'something 2'},{idx: 60, name:'something 3',matchingId:1 },{idx: 20, name:'something 4',matchingId:2},{idx: 25, name:'something 4',matchingId:2},{idx: 26, name:'something 4',matchingId:3}];
const third_array = first_array.map(el => ({
// add back all the original properties to the new object
...el,
// find the matching id -> if one exists, choose the index
matchingId: second_array.find(subEl => subEl.matchingId === el.id)?.idx
}));
console.log(third_array);
如果在特定部件上留下任何问题:
...
→命名的Destructuringfind(...)
?.
→命名的可选链接
如果您不能使用代码片段中的一些运算符,您可以查看Babel
来转换代码:https://babeljs.io/repl
这是这个片段的一个转译版本
我注意到second_array中缺少matchingId
,third_array 中缺少matchingIdFromSecond
在这种情况下,您可能必须决定要使用哪种联接类型。我已经分享了inner_join、的实现
const first_array = [
{ id: 1, name: 'something' },
{ id: 2, name: 'something 2' },
{ id: 3, name: 'something 3' },
{ id: 4, name: 'something 4' },
]
const second_array = [
{ idx: 40, name: 'something', matchingId: 1 },
{ idx: 30, name: 'something 2' },
{ idx: 60, name: 'something 3', matchingId: 1 },
{ idx: 20, name: 'something 4', matchingId: 2 },
{ idx: 25, name: 'something 4', matchingId: 2 },
{ idx: 26, name: 'something 4', matchingId: 3 },
]
const third_array = [
{ id: 1, name: 'something', matchingIdFromSecond: 40 },
{ id: 2, name: 'something 2' },
{ id: 3, name: 'something 3', matchingIdFromSecond: 40 },
{ id: 3, name: 'something 4', matchingIdFromSecond: 30 },
{ id: 4, name: 'something 4', matchingIdFromSecond: 2 },
{ id: 5, name: 'something 4', matchingIdFromSecond: 26 },
]
const inner_join = third_array.map((item3) => {
const matches2 = second_array.filter((val2) => val2.idx === item3.matchingIdFromSecond)
const matches21 = matches2.map(match2 => first_array.filter((val1) => val1.id === match2.matchingId))
return matches21.map((matches1, i) => (matches1.map(match1 => ({
...item3,
match1,
match2: matches2[i],
}))))
}).flat(2)
console.log('inner_join', inner_join)
对于更干净、更健壮的代码,更喜欢使用lodash-joins-npm库,在那里您可以通过模拟我的输出(match1
、match2
被吸收到父对象中(
const output2 = _.hashInnerJoin(
_.hashInnerJoin(third_array, 'matchingIdFromSecond', second_array, 'idx'),
'matchingId',
first_array,
'id'
)
编辑:我继续通过在SQL中运行相同的程序来验证输出。共享代码以供参考。
CREATE TABLE t1 (id INTEGER, name VARCHAR(30));
CREATE TABLE t2 (idx INTEGER, name VARCHAR(30), matchingId INTEGER);
CREATE TABLE t3 (id INTEGER, name VARCHAR(30), matchingIdFromSecond INTEGER);
INSERT INTO t1 VALUES (1, "something"), (2, "something 2"), (3, "something 3"), (4, "something 4");
INSERT INTO t2 VALUES (40,"something",1),(30,"something 2", NULL),(60,"something 3",1),(20,"something 4",2),(25,"something 4",2),(26,"something 4",3);
insert into t3 values (1,"something",40),(2,"something 2", NULL),(3,"something 3",40),(3,"something 4",30),(4,"something 4",2),(5,"something 4",26);
SELECT * from t3 join t2 on t3.matchingIdFromSecond = t2.idx join t1 on t1.id = t2.matchingId;