I am trying to make a array which have objects .Actually I need to push object in array but before I have some conditions
I have a array(a is array of objects) .I need to first remove all objects which have property "hidden": true, .I am able to do that like this
I have another b (b is is array of objects).in which I need to collect values from that using parameter fieldNameOrPath .Those value which are deleted from first array which have hidden :true need not to consider in second array .Not to check fieldNameOrPath .Or we can also delete those are deleted from first array using fieldNameOrPath
I trying to fetch values try to get expected result I fail to get
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)
}
}
Expected array
[{
Type__c: "pqr",
akritiv__So_Number__c: "a"
}, {
Type__c: "Invoice",
akritiv__So_Number__c: "-"
}, {
Type__c: "inc",
akritiv__So_Number__c: "c"
}, ]
here is fiddle http://jsfiddle.net/93m4wbh1/
There is no reason to delete elements from the array. Try this.
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);
You are pretty close to what you want i think.
I did some minor changes:
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.