将这两个对象合并为一个并添加键的最佳方法是什么。我已经尝试了很多,但我迷失了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....]
答案 0 :(得分:0)
var newList = [];
for(var i=0; i<obj1.length; i++){
newList.push({
Price: obj1[i],
Name: obj2[i]
});
}
这没有错误处理,并且使用数组的map函数可能有更好的方法,但只要两个原始对象的长度相同,这就应该有效。
答案 1 :(得分:0)
应该非常简单。假设arr1
是包含价格的数组,arr2
是包含名称的数组:
var newarr = [],
len = (number of items);
for(var i=0;i<len;i++){
newarr.push({
Price:arr1[i];
Name:arr2[i];
});
}
答案 2 :(得分: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);
答案 3 :(得分: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}
]