简体   繁体   English

基于键值数组将 javascript object 转换为对象数组

[英]convert javascript object into array of objects based on key value array

I have the following object structure in javascript我在 javascript 中有以下 object 结构

{
city: ["Gepps Cross", "Mooroolbark"],
collection_id: ["132", "3846"],
company: ["Wallis Drive-In", "Liz Skilbeck 0404 139 489"],
country: ["Australia", "Australia"],
dates: ["29/07/2020|10/08/2020|20/07/2020", "30/08/2020"],
name: ["METRO NORTH - Wallis Drive In", "MOOROOLBARK"],
state: ["South Australia", ""],
street_address: ["Wallis Drive-In - 588 Main North Road (enter at main gates)", "40 Orrong Road"],
zip: ["5094", "3138"]
}

I would like the data to be presented as an array of objects where the shape of object is exactly the same as the original but each value in the original key values is separated into a new object.我希望数据以对象数组的形式呈现,其中 object 的形状与原始键值完全相同,但原始键值中的每个值都被分成一个新的 object。

[
{
city: "Gepps Cross",
collection_id: "132",
company: "Wallis Drive-In",
country: "Australia",
dates: "29/07/2020|10/08/2020|20/07/2020",
name: "METRO NORTH - Wallis Drive In",
state: "South Australia",
street_address: "Wallis Drive-In - 588 Main North Road (enter at main gates)",
zip: "5094"
},
{
city: "Mooroolbark",
collection_id: "3846",
company: "Liz Skilbeck 0404 139 489",
country: "Australia",
dates: "30/08/2020",
name: "MOOROOLBARK",
state: "",
street_address: "40 Orrong Road"],
zip: "3138"
}
]

The esiest way is to map one of the array in object最简单的方法是 map object 中的阵列之一

 const obj = { city: ["Gepps Cross", "Mooroolbark"], collection_id: ["132", "3846"], company: ["Wallis Drive-In", "Liz Skilbeck 0404 139 489"], country: ["Australia", "Australia"], dates: ["29/07/2020|10/08/2020|20/07/2020", "30/08/2020"], name: ["METRO NORTH - Wallis Drive In", "MOOROOLBARK"], state: ["South Australia", ""], street_address: ["Wallis Drive-In - 588 Main North Road (enter at main gates)", "40 Orrong Road"], zip: ["5094", "3138"] } const result = obj.city.map((rec, index) => { return { city: obj.city[index], collection_id: obj.collection_id[index], company: obj.company[index], country: obj.country[index], dates: obj.dates[index], name: obj.name[index], state: obj.state[index], street_address: obj.street_address[index], zip: obj.zip[index] } }) console.log(result)

But if you have dinamacly array with different fields this way will be better但是,如果您有具有不同字段的 dinamacly 数组,这种方式会更好

 const obj = { city: ["Gepps Cross", "Mooroolbark"], collection_id: ["132", "3846"], company: ["Wallis Drive-In", "Liz Skilbeck 0404 139 489"], country: ["Australia", "Australia"], dates: ["29/07/2020|10/08/2020|20/07/2020", "30/08/2020"], name: ["METRO NORTH - Wallis Drive In", "MOOROOLBARK"], state: ["South Australia", ""], street_address: ["Wallis Drive-In - 588 Main North Road (enter at main gates)", "40 Orrong Road"], zip: ["5094", "3138"] } const result = obj.city.map((rec, index) => { const keys = Object.keys(obj) return keys.reduce((acc, element) => { return {...acc, [element]: obj[element][index] } }, {}) }) console.log(result)

This is one way to do it.这是一种方法。

You loop trough one random object's property (in this case city ).您循环遍历一个随机对象的属性(在本例中为city )。 This will give you as many loops as the resulting array's intended length.这将为您提供与结果数组的预期长度一样多的循环。

For each loop you parse all object's properties and save the value in a temporary object item .对于每个循环,您解析所有对象的属性并将值保存在临时 object item中。

Finally you push the item into the result array.最后,您将项目推送到result数组中。

 const obj = { city: ["Gepps Cross", "Mooroolbark"], collection_id: ["132", "3846"], company: ["Wallis Drive-In", "Liz Skilbeck 0404 139 489"], country: ["Australia", "Australia"], dates: ["29/07/2020|10/08/2020|20/07/2020", "30/08/2020"], name: ["METRO NORTH - Wallis Drive In", "MOOROOLBARK"], state: ["South Australia", ""], street_address: ["Wallis Drive-In - 588 Main North Road (enter at main gates)", "40 Orrong Road"], zip: ["5094", "3138"] } let result = []; obj.city.forEach( (el, i) => { let item = {}; for (const property in obj) { item[property] = obj[property][i]; } result.push(item); }); console.log(result);

Assuming values length is same.假设值长度相同。

let obj = {
city: ["Gepps Cross", "Mooroolbark"],
collection_id: ["132", "3846"],
company: ["Wallis Drive-In", "Liz Skilbeck 0404 139 489"],
country: ["Australia", "Australia"],
dates: ["29/07/2020|10/08/2020|20/07/2020", "30/08/2020"],
name: ["METRO NORTH - Wallis Drive In", "MOOROOLBARK"],
state: ["South Australia", ""],
street_address: ["Wallis Drive-In - 588 Main North Road (enter at main gates)", "40 Orrong Road"],
zip: ["5094", "3138"]
};

var keys = Object.keys(obj);
var values = Object.values(obj);
var valueslength = values[0].length;
var results = [];
for(var i=0; i<valueslength; i++){  
  var to = {};
  for(k of keys){
    to[k] = obj[k][i];    
  }  
  results.push(to);
  
}

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

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