【发布时间】:2021-08-09 09:53:53
【问题描述】:
我有两个不同的数组,如果对象 id 之间有可用的对象,我想从第二个数组中添加数组对象。
firstArray = [
{ id: 'LOA13', type: 'LOAD', otherProperty: 11 },
{ id: 'UNA1', type: 'UNLOAD', otherProperty: 22 },
{ id: 'LOA14', type: 'LOAD', otherProperty: 33 },
{ id: 'UNA15', type: 'UNLOAD', otherProperty: 44 }
]
secondArray = [
{ id: 'LOA13', type: 'LOAD', otherProperty: 12 },
{ id: '123', type: 'DRAG', otherProperty: 34 },
{ id: 'UNA1', type: 'UNLOAD', otherProperty: 56 },
{ id: 'LOA14', type: 'LOAD', otherProperty: 12 },
{ id: '456', type: 'DRAG', otherProperty: 1212 },
{ id: '789', type: 'DRAG', otherProperty: 9898 },
{ id: 'UNA15', type: 'UNLOAD', otherProperty: 56 }
]
如果在特定的 Ids 序列之间找到任何对象,那么它们应该添加到数组中的结果中。
预期的结果是这样的:
[
{ id: 'LOA13', type: 'LOAD', otherProperty: 11 },
{ id: '123', type: 'DRAG', otherProperty: 34 }, // ADDED as found between ID= LOA13 and UNA1
{ id: 'UNA1', type: 'UNLOAD', otherProperty: 22 },
{ id: 'LOA14', type: 'LOAD', otherProperty: 33 },
{ id: '456', type: 'DRAG', otherProperty: 1212 }, // ADDED as found between ID=LOA14 and UNA15
{ id: '789', type: 'DRAG', otherProperty: 9898 }, // ADDED as found between ID=LOA14 and UNA15
{ id: 'UNA15', type: 'UNLOAD', otherProperty: 44 }
]
【问题讨论】:
-
Id 没有特定的顺序。我只需要第二个数组中的任何对象在第一个数组的特定 id 之间,那么它应该在结果数组中。
标签: javascript arrays typescript