let inspecItem = [ { advice: "检安公司联系装置开作业票紧固或采取其他措施", id: "6", index: 0, method: "目测或报警仪检测", name: "法兰泄漏检查", ranking: 1, target: "F", }, { advice: "预紧力不足的报通知单并联系装置开作业票紧固", id: "7", index: 0, method: "定矩扳手扭矩复核", name: "预紧力检查", ranking: 2, target: "F", }, { advice: "轻度锈蚀告知装置重做法兰防腐;严重锈蚀的联系装置安排打包或更换法兰处理。", content: "", id: "2", image: "", index: 0, method: "目测", name: "法兰生锈检查", ranking: 3, target: "F", tempFilePaths: "" }, { advice: "严重锈蚀的联系装置更换螺栓", content: "", id: "3", image: "", index: 0, method: "目测", name: "螺栓生锈检查", ranking: 4, target: "F", tempFilePaths: "" }, , { advice: "严重锈蚀的联系装置更换螺栓", content: "", id: "7", image: "", index: 0, method: "目测", name: "螺栓生锈检查", ranking: 4, target: "F", tempFilePaths: "" } ] let flangeInspec = [ { id: "0a1ff74895c0d0460ae10bf20a2f1e87", itemId: 7, objectId: "727e618b70e1ebbd212b2dc2245d327c", resultDetail: null, resultId: null, resultImage: null, }, { id: "33d2ac7d3b90fb51a130e2082d4cff1b", itemId: 2, objectId: "727e618b70e1ebbd212b2dc2245d327c", resultDetail: null, resultId: null, resultImage: null }, { id: "33d2ac7d3b90fb51a130e2082d4cff1b", itemId: 6, objectId: "727e618b70e1ebbd212b2dc2245d327c", resultDetail: null, resultId: null, resultImage: null }, { id: "01fb8bd425f839e7ba7e3313a824b851", itemId: 3, objectId: "727e618b70e1ebbd212b2dc2245d327c", resultDetail: null, resultId: null, resultImage: null } ] let inspecList = [] 如果flangeInspec的itemId和inspecItem的id相等,就将inspecItem中的这条数据添加到inspecList中,请用forEach方法
时间: 2023-02-08 09:25:40 浏览: 63
您的问题是想问如何将 `flangeInspec` 中的数据与 `inspecItem` 中的数据进行匹配,然后将匹配成功的数据添加到 `inspecList` 中,是这样的吗?
如果是,您可以使用以下方法实现:
```
for (let i = 0; i < flangeInspec.length; i++) {
for (let j = 0; j < inspecItem.length; j++) {
if (flangeInspec[i].itemId === inspecItem[j].id) {
inspecList.push(inspecItem[j]);
break;
}
}
}
```
上面的代码会对 `flangeInspec` 数组中的每一条数据和 `inspecItem` 数组中的每一条数据进行匹配,如果匹配成功,就将匹配成功的数据添加到 `inspecList` 数组中。
相关问题
let inspecItem = [ { advice: "检安公司联系装置开作业票紧固或采取其他措施", id: "6", index: 0, method: "目测或报警仪检测", name: "法兰泄漏检查", ranking: 1, target: "F", }, { advice: "预紧力不足的报通知单并联系装置开作业票紧固", id: "7", index: 0, method: "定矩扳手扭矩复核", name: "预紧力检查", ranking: 2, target: "F", }, { advice: "轻度锈蚀告知装置重做法兰防腐;严重锈蚀的联系装置安排打包或更换法兰处理。", content: "", id: "2", image: "", index: 0, method: "目测", name: "法兰生锈检查", ranking: 3, target: "F", tempFilePaths: "" }, { advice: "严重锈蚀的联系装置更换螺栓", content: "", id: "3", image: "", index: 0, method: "目测", name: "螺栓生锈检查", ranking: 4, target: "F", tempFilePaths: "" } ] let flangeInspec = [ { id: "0a1ff74895c0d0460ae10bf20a2f1e87", itemId: 7, objectId: "727e618b70e1ebbd212b2dc2245d327c", resultDetail: null, resultId: null, resultImage: null, }, { id: "33d2ac7d3b90fb51a130e2082d4cff1b", itemId: 2, objectId: "727e618b70e1ebbd212b2dc2245d327c", resultDetail: null, resultId: null, resultImage: null }, { id: "33d2ac7d3b90fb51a130e2082d4cff1b", itemId: 2, objectId: "727e618b70e1ebbd212b2dc2245d327c", resultDetail: null, resultId: null, resultImage: null }, { id: "01fb8bd425f839e7ba7e3313a824b851", itemId: 3, objectId: "727e618b70e1ebbd212b2dc2245d327c", resultDetail: null, resultId: null, resultImage: null } ] let inspecList= [] inspecItem flangeInspec 如果flangeInspec 的itemId 和 inspecItem 的id 一样 就向 inspecItem 追加属性 index,content,image,tempFilePaths,并将inspecItem中的这条数据添加到inspecList中
你可以使用两层循环来实现这个功能。
首先,在第一层循环中,遍历 flangeInspec 数组中的每一项,然后在第二层循环中,遍历 inspecItem 数组中的每一项。
在第二层循环中,比较 flangeInspec 中当前项的 itemId 和 inspecItem 中当前项的 id 是否相同。如果相同,则向 inspecItem 中当前项追加 index、content、image、tempFilePaths 四个属性,并将 inspecItem 中当前项添加到 inspecList 中。
代码示例如下:
```
for (const item of flangeInspec) {
for (const obj of inspecItem) {
if (item.itemId === obj.id) {
obj.index = item.index;
obj.content = item.content;
obj.image = item.image;
obj.tempFilePaths = item.tempFilePaths;
inspecList.push(obj);
break;
}
}
}
```
let arr={0: 3, 1: 3} 把arr变成【3】
可以使用 Object.values() 方法将对象的值转为数组,然后获取数组的第一个元素即可得到结果。具体代码如下:
```
let arr = {0: 3, 1: 3};
arr = Object.values(arr);
// arr 现在为 [3, 3]
arr = arr.slice(0, 1);
// arr 现在为 [3]
```
注意,由于对象的属性名不是连续的数字,因此它不能被直接转换为数组。我们需要使用 Object.values() 方法来获取对象的值,然后再对数组进行操作。