如何使用不同的对象在javascript中创建对象数组?

时间:2015-06-13 06:06:49

标签: javascript jquery

我正在尝试创建一个有对象的数组。实际上我需要在数组中推送对象但是在我有条件之前

  • 我有一个数组(一个是对象数组)。我需要首先删除所有具有" hidden":true,的属性的对象/ strong>。我能够像这样做

  • 我有另一个 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"
}, ]

这里是小提琴 http://jsfiddle.net/93m4wbh1/

2 个答案:

答案 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)

你非常接近我的想法。

我做了一些小改动:

  • 首先我使用splice而不是delete来确保对象是 从数组中删除而不是留下空记录。
  • 然后我确保为每列创建和推送对象,而不是每列中的每条记录。
  • 最后我修复了一个防止小虫子的问题 要使用[](像数组一样)添加到对象的值。

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);