1

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"]}] 
3
  • can you specify what's the the logic of derivedArray ? it's all uniqe values from all products or what ? Commented May 23, 2016 at 18:58
  • @shershen I have added products with duplicate product_properties to clarify on expected handling of duplicates. The property_names and their corresponding values should be unique even if the original array has duplicates Commented May 23, 2016 at 20:24
  • I add duplicates filtering to my answer. Commented May 23, 2016 at 21:46

2 Answers 2

2

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"]}
]
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.