我需要检查三个不同的数组以查看它们的索引是否匹配。如果它们匹配,则创建一个对象。第三阵列可以在阵列中具有较少的项目。如果第三个数组的项目较少,则前两个数组应继续检查其索引是否匹配,并创建另一个对象。与“ seqIds”数组中的索引匹配的数组索引应添加“ seqId”作为属性,其他两个数组中与“ seqId”索引不匹配的索引不会获得“ seqId”属性。
编辑:invIds和invTypes数组的长度将始终相同。
示例数组:
invIds: [1, 2, 3, 4];
invTypes: ["A", "B", "C", "D"];
seqIds: [10, 11];
invs数组应包含以下对象:
invs: [
{
"invId": 1,
"invType": "A",
"seqId": 10
},
{
"invId": 2,
"invType": "B",
"seqId": 11
},
{
"invId": 3,
"invType": "C"
},
{
"invId": 4,
"invType": "D"
}
];
我写的for循环:
var invs = [];
for (var invI = 0; invI < this.state.invIds.length; invI++) {
for (var invT = 0; invT < this.state.invTypes.length; invT++) {
for (var invS = 0; invS < this.state.invSeqIds.length; invS++) {
if (invI === invT && invT === invS) {
invs.push({
seqId: this.state.invSeqIds[invS],
userId: this.state.invIds[invI],
invTypeCd: this.state.invTypes[invT],
importId: randInt
});
}
}
if (invI === invT) {
invs.push({
userId: this.state.invIds[invI],
invTypeCd: this.state.invTypes[invT],
importId: randInt
});
}
}
}
我编写的for循环未正确添加到数组中,它会这样做:
{"invId": 1, "invType": "A", "seqId": 10}
{"invId": 1, "invType": "A"}
{"invId": 2, "invType": "B", "seqId": 11}
{"invId": 2, "invType": "B"}
{"invId": 3, "invType": "C"}
{"invId": 4, "invType": "D"}
答案 0 :(得分:2)
解决此问题的另一种方法是使用while-statement
和运算符in
来检查源数组的索引。
let invIds = [1, 2, 3, 4],
invTypes = ["A", "B", "C", "D"],
seqIds = [10, 11],
result = [],
i = 0;
while (i in invIds && i in invTypes) {
result[i] = Object.create(null);
result[i].invId = invIds[i];
result[i].invType = invTypes[i];
if (i in seqIds) result[i].seqId = seqIds[i];
i++;
}
console.log(result);
.as-console-wrapper {max-height: 100% !important;top: 0;}
答案 1 :(得分:1)
这是您想要的吗?只需使用一个临时变量来存储对象属性并推送到invs。
如果seqIds
的元素数较少,请在分配tmp
的属性seqIds
之前检查是否存在。
let invIds = [1, 2, 3, 4], invTypes = ["A", "B", "C", "D"], seqIds = [10, 11];
var invs = [];
for (let i = 0; i< invIds.length; ++i) {
let tmp = {};
tmp.invIds = invIds[i];
tmp.invTypes = invTypes[i];
if(seqIds[i]) tmp.seqIds = seqIds[i];
invs.push(tmp);
}
console.log(invs);
答案 2 :(得分:1)
var invIds = [1, 2, 3, 4];
var invTypes = ["A", "B", "C", "D"];
var seqIds = [10, 11];
var invs = [];
for (var i = 0, length = invIds.length; i < length; i++) {
var inv = {
invId: invIds[i],
invType: invTypes[i]
};
if (i < seqIds.length) {
inv.seqId = seqIds[i];
}
invs.push(inv)
}
console.log(invs);
或js ES6方式
const invIds = [1, 2, 3, 4];
const invTypes = ["A", "B", "C", "D"];
const seqIds = [10, 11];
const invs = Array(invIds.length)
.fill(undefined).map((_, i) => {
const result = {
invId: invIds[i],
invType: invTypes[i]
};
if (seqIds.length > i) result.seqId = seqIds[i];
return result;
});
console.log(invs)
答案 3 :(得分:0)
尝试一下:
var invs = this.state.invIds.map(function(item, index){
var objReturned ={}
objReturned.invId = item;
if(this.state.invTypes[index]) objReturned.invType = this.state.invTypes[index]
if(this.state.invSeqIds[index]) objReturned.seqId = this.state.invSeqIds[index]
return objReturned;
} )
答案 4 :(得分:0)
我不建议使用嵌套循环,因为这样可能会造成混乱,并且很难理解和维护。我将其分解为以下步骤:
定义结果数组,您将在其中存储结果并找到最大的数据集
let invIds = [1, 2, 3, 4];
let invTypes = ["A", "B", "C", "D"];
let seqIds = [10, 11];
let result = [];
let maxLength = Math.max(invIds.length, invTypes.length, seqIds.length);
遍历从0到最大数据集末尾的所有索引,并在满足条件的情况下向结果数组添加新对象
for (let i = 0; i < maxLength; i++) {
let newItem = {};
// If the property exists and it meets your conditions
// Also keep in mind if any id is 0, you will have to add to the
// condition as 0 is a falsy value
if (invIds[i] || invIds[i] === 0) {
newItem.invId = invIds[i];
}
if (invTypes[i]) {
newItem.invType = invTypes[i];
}
if (seqIds[i] || seqIds[i] === 0) {
newItem.seqId = seqIds[i];
}
result.push(newItem);
}