0

将这两个对象合并为一个并添加键的最佳方法是什么。我已经尝试了很多,但我对 javascript 在对象和数组上的工作方式感到迷茫......谁能引导我走向正确的方向?使用纯 JS 或 jquery。

对象 1

["EUR 1,99", "EUR 0,99", "EUR 4,99", "EUR 2,29", "EUR 1,43", "EUR 1,60", "EUR 1,79", "EUR 1,79", "EUR 1,39", "EUR 6,30", "EUR 1,43", "EUR 1,78", "EUR 1,90", "EUR 1,24", "EUR 1,41"]

对象 2

["Popken", "Lucky Animal", "mein-terrarium", "zooup", "zoofair", "XL-Hundeshop", "tiierisch-de", "Zoo Galaxie", "Petshop", "Danto GmbH", "Heimtierbedarf-Mazinke", "TIERKOSMOS", "Gazoma", "Zooheld", "dasko24"]

所需对象

[[Price="EUR 1,99",Name="Popken"],[Price="EUR 0,99",Name="Lucky Animal"],[Price="EUR 4,99",Name="mein-terrarium"], etc....]  
4

4 回答 4

0
var newList = [];

for(var i=0; i<obj1.length; i++){
    newList.push({
        Price: obj1[i],
        Name: obj2[i]
    });
}

这没有错误处理,并且可能有更好的方法使用 Array 的 map 函数,但只要两个原始对象的长度相同,这应该可以工作。

于 2013-03-11T19:46:53.730 回答
0

应该很简单。假设arr1您的数组带有价格,并且arr2您的数组带有名称:

var newarr = [],
    len = (number of items);

for(var i=0;i<len;i++){
    newarr.push({
        Price:arr1[i];
        Name:arr2[i];
    });
}
于 2013-03-11T19:48:24.793 回答
0

在 underscore.js 中使用 zip 函数的解决方案

var prices = ["EUR 1,99", "EUR 0,99", "EUR 4,99", "EUR 2,29", "EUR 1,43", "EUR 1,60", "EUR 1,79", "EUR 1,79", "EUR 1,39", "EUR 6,30", "EUR 1,43", "EUR 1,78", "EUR 1,90", "EUR 1,24", "EUR 1,41"];

var products = ["Popken", "Lucky Animal", "mein-terrarium", "zooup", "zoofair", "XL-Hundeshop", "tiierisch-de", "Zoo Galaxie", "Petshop", "Danto GmbH", "Heimtierbedarf-Mazinke", "TIERKOSMOS", "Gazoma", "Zooheld", "dasko24"];

var result = _.map(_.zip(products, prices), function(arr) {
    return {
        Price: arr[1],
        Name: arr[0]
    };
});


console.log(result);
于 2013-03-11T19:53:26.613 回答
0

这是一个通用的解决方案——它将“折叠”一个对象,以便您获得对象列表而不是列表对象。

function refold_list_from_object(data) {
    "use strict"    
    var keys = Object.keys(data),
        descriptor = {configurable: true, enumerable: true, writable: true, value: null},
        properties = keys.reduce(function(p, k){
            p[k] = descriptor
            return p
        }, {}),
        nrecords = Math.max.apply(null, keys.map(function(k){return data[k].length})),
        records  = [], vi
    function makerecord() {
        return Object.create(null, properties)
    }
    function record_from_value_index(vi) {
        return keys.reduce(function(obj, k) {
            obj[k] = data[k][vi] || null
            return obj
        }, makerecord())
    }
    for (vi = 0; vi < nrecords; vi++) {
        records.push(record_from_value_index(vi))
    }
    return records
}

data应该是一个对象,列表由它们的字段名索引:

var data = {
    Price: ["EUR 1,99", "EUR 0,99", "EUR 4,99", "EUR 2,29", "EUR 1,43", "EUR 1,60", "EUR 1,79", "EUR 1,79", "EUR 1,39", "EUR 6,30", "EUR 1,43", "EUR 1,78", "EUR 1,90", "EUR 1,24", "EUR 1,41"],
    Name: ["Popken", "Lucky Animal", "mein-terrarium", "zooup", "zoofair", "XL-Hundeshop", "tiierisch-de", "Zoo Galaxie", "Petshop", "Danto GmbH", "Heimtierbedarf-Mazinke", "TIERKOSMOS", "Gazoma", "Zooheld", "dasko24"],
    // An extra field to demonstrate padding:
    Description: ['Foo', 'Bar', 'Baz']
}

var recordlist = refold_list_from_object(data)

recordlist看起来像这样:

[
  {"Price":"EUR 1,99", "Name":"Popken",         "Description":"Foo"},
  {"Price":"EUR 0,99", "Name":"Lucky Animal",   "Description":"Bar"},
  {"Price":"EUR 4,99", "Name":"mein-terrarium", "Description":"Baz"},
  {"Price":"EUR 2,29", "Name":"zooup",          "Description":null},
  {"Price":"EUR 1,43", "Name":"zoofair",        "Description":null},
  {"Price":"EUR 1,60", "Name":"XL-Hundeshop",   "Description":null},
  {"Price":"EUR 1,79", "Name":"tiierisch-de",   "Description":null},
  {"Price":"EUR 1,79", "Name":"Zoo Galaxie",    "Description":null},
  {"Price":"EUR 1,39", "Name":"Petshop",        "Description":null},
  {"Price":"EUR 6,30", "Name":"Danto GmbH",     "Description":null},
  {"Price":"EUR 1,43", "Name":"Heimtierbedarf-Mazinke", "Description":null},
  {"Price":"EUR 1,78", "Name":"TIERKOSMOS",     "Description":null},
  {"Price":"EUR 1,90", "Name":"Gazoma",         "Description":null},
  {"Price":"EUR 1,24", "Name":"Zooheld",        "Description":null},
  {"Price":"EUR 1,41", "Name":"dasko24",        "Description":null}
]
于 2013-03-11T22:05:19.417 回答