简体   繁体   English

将JavaScript数组中的对象合并为对象

[英]Merge Objects in a JavaScript Array to object

I am trying to create a single JavaScript object from an array of objects. 我试图从一个对象数组创建一个JavaScript对象。

Some values in the object remain the same, while the numerical or float values need to be summed up to form the new single object. 对象中的某些值保持不变,而数值或浮点值需要相加以形成新的单个对象。

How can this be achieved? 怎么能实现这一目标?

[{
    'visit_date': '2013-02-02',
    'house_number': '22',
    'price': 12.98,
    'count_value': 21.554,
    'variance': -23.434343434
},
{
    'visit_date': '2013-02-02',
    'house_number': '22',
    'price': 34.78,
    'count_value': 1.34,
    'variance': -23.434343434
},
{
    'visit_date': '2013-02-02',
    'house_number': '22',
    'price': 61.41,
    'count_value':1.94,
    'variance': -12.977677874
}]

Loop through the array, use a for-in loop on each object, and for entries where typeof value === "number" , sum them up. 循环遍历数组,在每个对象上使用for-in循环,对于typeof value === "number"条目,将它们相加。 Presumably do something useful when they aren't numbers, but you haven't said what. 当他们不是数字时,可能会做一些有用的事情,但你还没有说出什么。 :-) :-)

Sorry, missed the jQuery tag. 对不起,错过了jQuery标签。 It can be shorter with jQuery's each : each jQuery都可以更短:

var dest = {};

$.each(source, function(index, entry) {
    $.each(entry, function(key, value) {
        if (typeof value === "number") {
            dest[key] = key in dest ? dest[key] + value : value;
        }
        else {
            // do something useful with non-numbers
        }
     });
});

each will loop through the array elements if you give it an array, or through the properties of an object if you give it a non-array object. 如果给它一个数组, each将循环遍历数组元素,如果给它一个非数组对象,则通过对象的属性循环。 So in the above, the outer each loops through your array, and the inner each loops through each object's properties. 所以在上面,外部each循环遍历你的数组,内部each循环遍历每个对象的属性。

The in operator on this line: in这条线上运营商:

dest[key] = key in dest ? dest[key] + value : value;

...tells us whether dest already has a key with that name: If so, we add the value to it. ...告诉我们dest是否已经有一个具有该名称的密钥:如果是,我们添加到它。 If not, we create a new property for that key using the value. 如果没有,我们使用该值为该键创建一个新属性。


Original non-jQuery: 原始的非jQuery:

Roughly : 粗略地说

var dest = {};
var entry;
var index;
var key;
var value;

for (index = 0; index < source.length; ++index) {
    entry = source[index];
    for (key in entry) {
        value = entry[key];
        if (typeof value === "number") {
            dest[key] = key in dest ? dest[key] + value : value;
        }
        else {
            // do something useful with non-numbers
        }
    }
}

You can use the reduce function to achieve it 您可以使用reduce函数来实现它

var result = a.reduce(function (acc, c) {
    acc.visit_date = c.visit_date;
    acc.house_number = c.house_number;
    acc.price += c.price;
    acc.count_value += c.count_value;
    acc.variance += c.variance;
    return acc;
}, {
    'visit_date': '',
    'house_number': '',
    'price': 0,
    'count_value': 0,
    'variance': 0
});

The result will be 结果将是

{
    visit_date: "2013-02-02",
    house_number: "22",
    price: 109.17,
    count_value: 24.834,
    variance: -59.846364742
}

Something like this perhaps? 也许这样的事情? http://jsfiddle.net/tQLy5/1/ http://jsfiddle.net/tQLy5/1/

function aToObject(a) {
    var o = {}, l = a.length;
    while (l--) {
        for (key in a[l]) {
            if (o.hasOwnProperty(key) && typeof (o[key]) === 'number') {
                o[key] += a[l][key];   
            } else {
                 o[key] = a[l][key];   
            }
        }
    }
    return o;
}

Just iterate over the whole array and then over the properties of the objects in array. 只需迭代整个数组,然后遍历数组中对象的属性。 If the property is number you add them together, otherwise you just assign the property to the new object. 如果属性是数字,则将它们一起添加,否则只需将属性分配给新对象即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM