ahmd a ahmd a - 6 months ago 9x
Javascript Question

Javascript Object and Array

I have item array and it has 2 items as objects like this:

var item = [{
name: "audi",
body: "s1",
type: "cedan",
price: 10,
color: "red",
size: "large",
shape: "circle"
}, {
name: "bmw",
body: "x1",
type: "suv",
price: 20,
color: "blue",
size: "medium",
shape: "square"

how can i generate the following arrays from the item array, i mean if i add new item to item array i want it to be included to the new arrays below automaticlay

var name = ["audi","bmw"];
var body = ["s1","x1"];
var color = ["red","blue"];
var size = ["large","medium"];
var shape = ["square","circle"];
var type = ["cedan","suv"];
var price = [10,20];


If you want to generate a map of keys and values as array of values for each of those keys, then try

var output = {};
    output[key] = output[key] || [];
    if( output[key].indexOf(val[key]) == -1 )

Now you can fetch the array of values as you like

var names = output["name"];