我正在尝试创建一个有对象的数组。实际上我需要在数组中推送对象但是在我有条件之前
我有一个数组(一个是对象数组)。我需要首先删除所有具有" hidden":true,
我有另一个 b (b是对象数组)。我需要使用参数 fieldNameOrPath 从中收集值。这些值是从第一个数组删除 hidden:true 不需要考虑第二个数组。不检查 fieldNameOrPath 。或者我们也可以删除那些从第一个数组中删除的 fieldNameOrPath
我试图获取值尝试获得预期结果我无法获得
var deletedfieldNameOrPath=[ ];
for (var i = 0; i < a.length; i++) {
if (a[i].hidden) {
deletedfieldNameOrPath.push(a[i].fieldNameOrPath)
delete a[i]
}
}
console.log(a);
console.log(deletedfieldNameOrPath);
var objectarray = []
for (var i = 0; i < b.length; i++) {
for (var k = 0; k < b[i].columns.length; k++) {
var obj = {};
if (deletedfieldNameOrPath.indexOf(b[i].columns.fieldNameOrPath) == -1) {
obj.b[i].columns.fieldNameOrPath = b[i].columns.value;
}
objectarray.push(obj)
}
}
预期数组
[{
Type__c: "pqr",
akritiv__So_Number__c: "a"
}, {
Type__c: "Invoice",
akritiv__So_Number__c: "-"
}, {
Type__c: "inc",
akritiv__So_Number__c: "c"
}, ]
答案 0 :(得分:1)
没有理由从数组中删除元素。 试试这个。
var a = [{
"hidden": true,
"fieldNameOrPath": "Name",
}, {
"hidden": true,
"fieldNameOrPath": "akritiv__Account__r.Name",
}, {
"hidden": false,
"fieldNameOrPath": "Type__c",
}, {
"hidden": false,
"fieldNameOrPath": "akritiv__So_Number__c",
}];
var collectNameOrPath =
a.filter(function(o) { return !o.hidden })
.map(function(o) { return o.fieldNameOrPath });
console.log(collectNameOrPath);
var b = [{
"columns": [{
"value": "a0RK0000002l3AB",
"fieldNameOrPath": "Name"
}, {
"value": "Sun Life Financial",
"fieldNameOrPath": "akritiv__Account__r.Name"
}, {
"value": "pqr",
"fieldNameOrPath": "Type__c"
}, {
"value": "a",
"fieldNameOrPath": "akritiv__So_Number__c"
}]
}, {
"columns": [{
"value": "a0RK0000002l3ac",
"fieldNameOrPath": "Name"
}, {
"value": "Scottish Power",
"fieldNameOrPath": "akritiv__Account__r.Name"
}, {
"value": "Invoice",
"fieldNameOrPath": "Type__c"
}, {
"value": "-",
"fieldNameOrPath": "akritiv__So_Number__c"
}]
}, {
"columns": [{
"value": "a0RK0000002l3aC",
"fieldNameOrPath": "Name"
}, {
"value": "FirstEnergy",
"fieldNameOrPath": "akritiv__Account__r.Name"
}, {
"value": "inc",
"fieldNameOrPath": "Type__c"
}, {
"value": "c",
"fieldNameOrPath": "akritiv__So_Number__c"
}]
}]
var nameOrPathValues = b.map(function(o) {
var result = {};
o.columns.forEach(function(c) {
result[c.fieldNameOrPath] = c.value;
});
return result;
});
console.log(nameOrPathValues);
var objectarray = nameOrPathValues.map(function(o) {
var result = {};
collectNameOrPath.forEach(function(name) {
result[name] = o[name];
});
return result;
});
console.log(objectarray);
答案 1 :(得分:1)
你非常接近我的想法。
我做了一些小改动:
var a = [{
"hidden": true,
"fieldNameOrPath": "Name",
}, {
"hidden": true,
"fieldNameOrPath": "akritiv__Account__r.Name",
}, {
"hidden": false,
"fieldNameOrPath": "Type__c",
}, {
"hidden": false,
"fieldNameOrPath": "akritiv__So_Number__c",
}];
var deletedfieldNameOrPath = [];
var collectNameOrPath = [];
for (var i = 0; i < a.length; i) {
if (a[i].hidden) {
deletedfieldNameOrPath.push(a[i].fieldNameOrPath)
a.splice(i, 1);
continue;
} else {
collectNameOrPath.push(a[i].fieldNameOrPath);
}
i ++;
}
console.log(a);
console.log(deletedfieldNameOrPath);
[{
Type__c: "pqr",
akritiv__So_Number__c: "a"
}, {
Type__c: "Invoice",
akritiv__So_Number__c: "-"
}, {
Type__c: "inc",
akritiv__So_Number__c: "c"
},
]
var b = [{
"columns": [{
"value": "a0RK0000002l3AB",
"fieldNameOrPath": "Name"
}, {
"value": "Sun Life Financial",
"fieldNameOrPath": "akritiv__Account__r.Name"
}, {
"value": "pqr",
"fieldNameOrPath": "Type__c"
}, {
"value": "a",
"fieldNameOrPath": "akritiv__So_Number__c"
}]
}, {
"columns": [{
"value": "a0RK0000002l3ac",
"fieldNameOrPath": "Name"
}, {
"value": "Scottish Power",
"fieldNameOrPath": "akritiv__Account__r.Name"
}, {
"value": "Invoice",
"fieldNameOrPath": "Type__c"
}, {
"value": "-",
"fieldNameOrPath": "akritiv__So_Number__c"
}]
}, {
"columns": [{
"value": "a0RK0000002l3aC",
"fieldNameOrPath": "Name"
}, {
"value": "FirstEnergy",
"fieldNameOrPath": "akritiv__Account__r.Name"
}, {
"value": "inc",
"fieldNameOrPath": "Type__c"
}, {
"value": "c",
"fieldNameOrPath": "akritiv__So_Number__c"
}]
}]
var objectarray = []
for (var i = 0; i < b.length; i++) {
var obj = {};
for (var k = 0; k < b[i].columns.length; k++) {
if (deletedfieldNameOrPath.indexOf(b[i].columns[k].fieldNameOrPath) == -1) {
obj[b[i].columns[k].fieldNameOrPath] = b[i].columns[k].value;
}
}
objectarray.push(obj)
}
console.log(objectarray);