HattrickNZ HattrickNZ - 3 days ago 5
Javascript Question

formatting data using the map function + getting an array inside an array but don't want

I am trying to use map to fomat my data in a certain way.

what I want('values' is an array of objects):

[{name:"testmodel",values[{val1:"val1",...,val5:"val5"}]},
{name:"testmodel2",values[{val1:"val1",...,val5:"val5"}]},
...
{name:"testmodelN",values[{val1:"val1",...,val5:"val5"}]}
]


what I am getting('values' is an array of objects in side an array):

[{name:"testmodel",values[[{val1:"val1",...,val5:"val5"}]]},
{name:"testmodel2",values[[{val1:"val1",...,val5:"val5"}]]},
...
{name:"testmodelN",values[[{val1:"val1",...,val5:"val5"}]]}
]


this is my data

data_p = [
[ {"key":"Device_Model","value":"test_model"},
{"key":">20MB/30","value":"11"},
{"key":">200MB/30","value":"33"},
{"key":">2048MB/30","value":"10"},
{"key":">5120MB/30","value":"55"},
{"key":">10240MB/30","value":"10"}
],
[{"key":"Device_Model","value":"0P6B670"},{"key":">20MB/30","value":"9"},{"key":">200MB/30","value":"8"},{"key":">2048MB/30","value":"2"},{"key":">5120MB/30","value":"23"},{"key":">10240MB/30","value":"23"}],
[{"key":"Device_Model","value":"0P6B680"},{"key":">20MB/30","value":"1"},{"key":">200MB/30","value":"23"},{"key":">2048MB/30","value":"23"},{"key":">5120MB/30","value":"23"},{"key":">10240MB/30","value":"23"}],
[{"key":"Device_Model","value":"0P6B810"},{"key":">20MB/30","value":"5"},{"key":">200MB/30","value":"4"},{"key":">2048MB/30","value":"1"},{"key":">5120MB/30","value":"1"},{"key":">10240MB/30","value":"1"}],
[{"key":"Device_Model","value":"0P6B900"},{"key":">20MB/30","value":"4"},{"key":">200MB/30","value":"4"},{"key":">2048MB/30","value":"1"},{"key":">5120MB/30","value":"23"},{"key":">10240MB/30","value":"23"}]]


thsi is my map function:

var bar = color.domain().map(function(name){
return {
name: name,
values: data_p.map(function(d){
a=[]
a.push(d[1])
a.push(d[2])
a.push(d[3])
a.push(d[4])
a.push(d[5])
return a;
})
}
})


the above wont work as the
color
library is not available here but this should give you what I am getting

data_p.map(function(d){
a=[]
a.push(d[1])
a.push(d[2])
a.push(d[3])
a.push(d[4])
a.push(d[5])
return a;
})


this is what I get:

bar
[Object { name="test_model", values=[5]}, Object { name="0P6B670", values=[5]}, Object { name="0P6B680", values=[5]}, Object { name="0P6B810", values=[5]}, Object { name="0P6B900", values=[5]}]
bar[0]["values"]
[[Object { key=">20MB/30", value="11"}, Object { key=">200MB/30", value="33"}, Object { key=">2048MB/30", value="10"}, Object { key=">5120MB/30", value="55"}, Object { key=">10240MB/30", value="10"}]],...
bar[0]["values"][0]
[Object { key=">20MB/30", value="11"}, Object { key=">200MB/30", value="33"}, Object { key=">2048MB/30", value="10"}, Object { key=">5120MB/30", value="55"}, Object { key=">10240MB/30", value="10"}]


above i am very close but in side
bar[0]["values"]
I have an array of objects inside the values array. What I want is
values
to be an array of objects.

this is what I want:(note that this
values
is an array of objects)

bar[0]["values"]
[Object { key=">20MB/30", value="11"}, Object { key=">200MB/30", value="33"}, Object { key=">2048MB/30", value="10"}, Object { key=">5120MB/30", value="55"}, Object { key=">10240MB/30", value="10"}]





EDIT1



this is what I want

[{name:"testmodel",values[{ key=">20MB/30", value="11"}, { key=">200MB/30", value="33"}, { key=">2048MB/30", value="10"}, { key=">5120MB/30", value="55"}, { key=">10240MB/30", value="10"}]},
{name:"testmodel2",values[{ key=">20MB/30", value="11"}, { key=">200MB/30", value="33"}, { key=">2048MB/30", value="10"}, { key=">5120MB/30", value="55"}, { key=">10240MB/30", value="10"}]},
...
{name:"testmodelN",values[[{val1:"val1",...,val5:"val5"}]]}
]


when I use what you say I get this, the 25 key/values for each name. I jsut want 5 key/vals per name.

[{name:"testmodel",values[{val1:"val1",...,val25:"val25"}]},
{name:"testmodel2",values[{val1:"val1",...,val25:"val25"}]},
...
{name:"testmodelN",values[{val1:"val1",...,val25:"val25"}]}
]





EDIT2



tks to @trincot Answer below, broke this down just for my reference.
did the below in the console.

//1
aa=data_p.filter(function(d){
return d[0].value == "test_model";
})
aa
//2
aa=data_p.filter(function(d){
return d[0].value == "test_model";
})[0]
aa
//3
aa=data_p.filter(function(d){
return d[0].value == "test_model";
})[0].slice(1)
aa

Answer

You need to filter the data by the name you pass in the outer callback function. This you can do with filter (or in modern browsers with find). On the filtered item you can then take the elements with slice so that you exclude the first element containing the name:

var bar = color.domain().map(function(name){            
    return {            
      name: name,     
      values: data_p.filter(function(d){
        return d[0].value == name;
      })[0].slice(1)
    }
})
Comments