简体   繁体   中英

Derive an array from values of another array

I have this products array in an angular 2 component:

products = [{name: "product1", product_properties: [{name: "color", value: "blue"}, {name: "size", value: "small"}]}, 
            {name: "product2", product_properties: [{name: "color", value: "yellow"}, {name: "size", value: "medium"]}, 
            {name: "product3", product_properties: [{name: "color", value: "green"}, {name: "size", value: "large"}, 
            {name: "product4", product_properties: [{name: "color", value: "green"}, {name: "size", value: "small"}]} 
            {name: "product5", product_properties: [{name: "color", value: "yellow"}, {name: "size", value: "medium"}]

What is the most efficient way to loop over this array and derive an array which looks like the one below using typescript or javascript:

derivedArray = [{property_name: "color", values: ["blue", "yellow", "green"]}, 
                {property_name: "size", values: ["small", "medium", "large"]}] 

Your array has some syntax errors, I fix them:

var inData = [
  {name: "product1", product_properties:[{name: "color", value: "blue"}, {name: "size", value: "small"} ]}, 
  {name: "product2", product_properties:[{name: "color", value: "yellow"}, {name: "size", value: "medium"}]}, 
  {name: "product3", product_properties:[{name: "color", value: "green"},  {name: "size", value: "large"}]}
];

Let's build hashmap of property => values :

var hash = inData.reduce((acc, p) => {
  p.product_properties.forEach(prop => {
    if (!acc[prop.name]) acc[prop.name] = [];
    if (!~acc[prop.name].indexOf(prop.value)) // filter duplicates
      acc[prop.name].push(prop.value);});
    return acc;
}, {});

And now build requested data structure:

Object.keys(hash).map(key => ({property_name: key, values: hash[key]}))

Result is:

[
  {"property_name":"color","values":["blue","yellow","green"]},     
  {"property_name":"size","values":["small","medium","large"]}
]

Using jQuery

 var products = [{name: "product1", product_properties: [{name: "color", value: "blue"}, {name: "size", value: "small"}]}, {name: "product2", product_properties: [{name: "color", value: "yellow"}, {name: "size", value: "medium"}]}, {name: "product3", product_properties: [{name: "color", value: "green"}, {name: "size", value: "large"}]}]; var colorArray = []; var sizeArray = []; $.each(products,function(i,v){ var colorAttributes = v.product_properties[0]; var sizeAttributes = v.product_properties[1]; // check if attribute is already passed in the array using jQuery.inArray() if(colorAttributes.name=="color" && $.inArray(colorAttributes.value,colorArray)==-1){ colorArray.push(colorAttributes.value); } if(sizeAttributes.name=="size" && $.inArray(sizeAttributes.value,sizeArray)==-1){ sizeArray.push(sizeAttributes.value); } }); var derivedArray = [{property_name: "color", values: colorArray}, {property_name: "size", values: sizeArray}] ; alert(JSON.stringify(derivedArray )); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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