0

My Array is

[Object { Color="Blues",  Shape="Octagon",  Thickness="High (Over 1 Inch)",  more...}, Object { Color="Burgundys",  Shape="Oval",  Thickness="3⁄8" (approx.)",  more...}]

I want Output :

[{"Color":["Blues","Burgundys "],"Shape":['Octagon',"Oval"]}] 

Same for others values

2
  • And the language is? Commented Nov 30, 2016 at 18:26
  • Javascript Language Commented Nov 30, 2016 at 18:26

5 Answers 5

2

I'd approach this by iterating over the keys of each object, and adding the key as a hash to your values object.

var vals = {}
var src = [{ Color="Blues",  Shape="Octagon",  Thickness="High (Over 1 Inch)"}, { Color="Burgundys",  Shape="Oval",  Thickness="3⁄8 (approx.)"}]

src.forEach( function( obj ){

    for( var key in obj ){
        if( vals[ key ] === undefined ) 
            vals[ key ] = []

        vals[ key ].push( obj[ key ])
    }

})
Sign up to request clarification or add additional context in comments.

2 Comments

Object { Color=[1], Shape=[1], Thickness=[1], more...} It's giving this object with balnk values
I just copied the source array from your code. Obviously this will throw a run-time error, but was too lazy to properly format your src array. Fixed it now.
0

This should do what you want:

a = [{foo: 1, bar: 2}, {foo: 3, bar: 4}]
a.reduce((acc, val) => {
  for (var key in val) {
    if (!val.hasOwnProperty(key)) continue;
    if (!acc.hasOwnProperty(key)) acc[key] = []
    acc[key].push(val[key])
  }
  return acc
}, {})

1 Comment

nice use of the Array.prototype.reduce :)
0

It looks like you will have to do a loop to get what you are looking for.

var colors = [];
var shapes = []; for(var i = 0;i<objArray.length;i++)
{
    colors[i] = objArray[i].color
    shapes[i] = objArray[i].shape
}
answer = {};
answer.colors = colors;
answer.shapes = shapes;

Comments

0

You will have too loop through the object, and store the unique results. Following is an approximate way to code this:

var colors = [], shapes = [];

for (var i = 0; i < object.length; i++) {
  var color = object[i].Color, shape = object[i].Shape;
  if (colors.indexOf(color) === -1) { colors.push(color); }
  if (shapes.indexOf(shape) === -1) { shapes.push(shape); }
}

result = {"Color": colors, "Shape": shapes};

Comments

0
var arr = [{
  color: "Blues",
  Shape: "Octagon"
},
{
 color: "Burgundys",
 Shape="Oval"
}]

var targetObject = {};

for(var iloop=0; iloop< arr.length; iloop++){
  //get the keys in your object
  var objectKeys = Object.keys(arr[iloop]);

   //loop over the keys of the object
   for(var jloop=0; jloop<objectKeys.length; jloop++){
     //if the key is present in your target object push in the array 
     if( targetObject[ objectKeys[jloop] ] ){
       targetObject[objectKeys[jloop]].push( arr[iloop][objectKeys[jloop]] );
     }else{
       // else create a array and push inside the value
       targetObject[objectKeys[jloop]] = []
       targetObject[objectKeys[jloop]].push( arr[iloop][objectKeys[jloop]]     );
     }
   }
}

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.