将 json 的每个对象的一组值推送到不同的数组中
push set of values for each object of json into different arrays
目前我有以下代码,一个 JSON 文件,并使用下划线将每个外观的产品 ID 推送到一个数组中,我想做的是推送每个产品的产品 ID查看他们自己的数组,所以我会有 productArray1
、productArray2
、productArray3
等。我该如何调整它?
JSON
looks = [{
"id": "look1",
"products": ["hbeu50271385", "hbeu50274296", "hbeu50272359", "hbeu50272802"]
}, {
"id": "look2",
"products": [
"hbeu50274106", "hbeu50273647", "hbeu50274754", "hbeu50274063", "hbeu50274911", "hbeu50274106", "hbeu50240022", "hbeu50271944"
]
}, {
"id": "look3",
"products": [
"hbeu50272935", "hbeu50274426", "hbeu50271624", "hbeu50274762", "hbeu50275366", "hbeu50274433", "hbeu50262002", "hbeu50272364", "hbeu50272359"
]
}
.......
]
JS(下划线)
var productArray = []
_.each(looks, function(look) {
_.each(look.products, function(product) {
productArray.push(product.replace(/_.*/, ''))
})
})
我可能理解错了,你是问如何创建数组的数组吗?
var productArrays = [];
_.each(looks, function(look) {
var productArray = [];
_.each(look.products, function(product) {
productArray.push(product.replace(/_.*/, ''))
})
productArrays.push(productArray);
});
例如:http://jsfiddle.net/bvaughn/acam2rzo/
此代码将生成如下所示的二维数组:
[
["hbeu50271385"1: "hbeu50274296"2: "hbeu50272359"3: "hbeu50272802"],
["hbeu50274106"1: "hbeu50273647"2: "hbeu50274754"3: "hbeu50274063"4: "hbeu50274911"5: "hbeu50274106"6: "hbeu50240022"7: "hbeu50271944"length],
["hbeu50272935"1: "hbeu50274426"2: "hbeu50271624"3: "hbeu50274762"4: "hbeu50275366"5: "hbeu50274433"6: "hbeu50262002"7: "hbeu50272364"8: "hbeu50272359"]
];
类似这样的方法可行,productArrays
将包含您的每个产品数组,使用它们的 ID 作为键,例如:productArrays.look1
包含 look1 的所有产品。
var productArrays = {};
_.each(looks, function (look) {
productArrays[look.id] = [];
_.each(look.products, function (product) {
productArrays[look.id].push(product.replace(/_.*/, ''));
});
});
让我们构建一个 [name, products]
对的列表,然后将该列表传递给 _.object
以输出一个对象,其中 productArrayn
作为键,所需的产品数组作为值:
var res = _.chain(looks).
map(function(look) {
return [
"productArray" + look.id.substr(4),
_.map(look.products, function(product) {
return product.replace(/_.*/, '');
})
];
}).
object().
value();
会输出
productArray1: ["hbeu50271385", "hbeu50274296", ...],
productArray2: ["hbeu50274106", "hbeu50273647", ...],
productArray3: ["hbeu50272935", "hbeu50274426", ...],
....
目前我有以下代码,一个 JSON 文件,并使用下划线将每个外观的产品 ID 推送到一个数组中,我想做的是推送每个产品的产品 ID查看他们自己的数组,所以我会有 productArray1
、productArray2
、productArray3
等。我该如何调整它?
JSON
looks = [{
"id": "look1",
"products": ["hbeu50271385", "hbeu50274296", "hbeu50272359", "hbeu50272802"]
}, {
"id": "look2",
"products": [
"hbeu50274106", "hbeu50273647", "hbeu50274754", "hbeu50274063", "hbeu50274911", "hbeu50274106", "hbeu50240022", "hbeu50271944"
]
}, {
"id": "look3",
"products": [
"hbeu50272935", "hbeu50274426", "hbeu50271624", "hbeu50274762", "hbeu50275366", "hbeu50274433", "hbeu50262002", "hbeu50272364", "hbeu50272359"
]
}
.......
]
JS(下划线)
var productArray = []
_.each(looks, function(look) {
_.each(look.products, function(product) {
productArray.push(product.replace(/_.*/, ''))
})
})
我可能理解错了,你是问如何创建数组的数组吗?
var productArrays = [];
_.each(looks, function(look) {
var productArray = [];
_.each(look.products, function(product) {
productArray.push(product.replace(/_.*/, ''))
})
productArrays.push(productArray);
});
例如:http://jsfiddle.net/bvaughn/acam2rzo/
此代码将生成如下所示的二维数组:
[
["hbeu50271385"1: "hbeu50274296"2: "hbeu50272359"3: "hbeu50272802"],
["hbeu50274106"1: "hbeu50273647"2: "hbeu50274754"3: "hbeu50274063"4: "hbeu50274911"5: "hbeu50274106"6: "hbeu50240022"7: "hbeu50271944"length],
["hbeu50272935"1: "hbeu50274426"2: "hbeu50271624"3: "hbeu50274762"4: "hbeu50275366"5: "hbeu50274433"6: "hbeu50262002"7: "hbeu50272364"8: "hbeu50272359"]
];
类似这样的方法可行,productArrays
将包含您的每个产品数组,使用它们的 ID 作为键,例如:productArrays.look1
包含 look1 的所有产品。
var productArrays = {};
_.each(looks, function (look) {
productArrays[look.id] = [];
_.each(look.products, function (product) {
productArrays[look.id].push(product.replace(/_.*/, ''));
});
});
让我们构建一个 [name, products]
对的列表,然后将该列表传递给 _.object
以输出一个对象,其中 productArrayn
作为键,所需的产品数组作为值:
var res = _.chain(looks).
map(function(look) {
return [
"productArray" + look.id.substr(4),
_.map(look.products, function(product) {
return product.replace(/_.*/, '');
})
];
}).
object().
value();
会输出
productArray1: ["hbeu50271385", "hbeu50274296", ...],
productArray2: ["hbeu50274106", "hbeu50273647", ...],
productArray3: ["hbeu50272935", "hbeu50274426", ...],
....