Reputation: 6166
I have two different arrays which have the same length.
myData = [["site_1", 3722, 2938, 2529, 3419, 3495, 3477],
["site_2", 115, 55, 56, 111, 113, 124],
["site_3", 13, 11, 10, 17, 17, 16],
["site_4", 16, 11, 10, 17, 17, 16],
["site_5", 0, 0, 0, 0, 0, 0]
];
booleanData = [true, false, false, true, true];
I'm already using the first array to compute some data and I want to use the second one to add that information for each of the elements of the array.
_.chain(myCtrl.chartData.myData).forEach(data => {
myCtrl.legendData.push({
siteName: data[0],
color: myCtrl.chartData.chart.color(data[0])
});
}).value();
The above code works fine, it goes through the array and gets the string from each sub-array and computes a color for it.
I want to add the booleanData
and I tried it like this:
_.chain(myCtrl.chartData.myData).forEach(data => {
myCtrl.legendData.push({
siteName: data[0],
color: myCtrl.chartData.chart.color(data[0]),
hasIt: myCtrl.chartData.booleanData[data]
});
}).value();
I understand that the parameter should not be [data]
but I don't know how to do it to work fine.
The result I want in this case is that legendData
to be:
[ {siteName: "name_1", color: "#4ae412", hasIt: true},
{siteName: "name_2", color: "#4ae412", hasIt: false},
{siteName: "name_3", color: "#4ae412", hasIt: false},
{siteName: "name_4", color: "#4ae412", hasIt: true},
{siteName: "name_5", color: "#4ae412", hasIt: true}
]
Color has no effect on this, you can ignore it.
Do you have any idea?
Upvotes: 1
Views: 65
Reputation: 1082
Using forEach and keeping strictly as you have and asked, you can get your results as such:
var myData = [["site_1", 3722, 2938, 2529, 3419, 3495, 3477],
["site_2", 115, 55, 56, 111, 113, 124],
["site_3", 13, 11, 10, 17, 17, 16],
["site_4", 16, 11, 10, 17, 17, 16],
["site_5", 0, 0, 0, 0, 0, 0]
];
var booleanData = [true, false, false, true, true], legendData = [];
myData.forEach(function(x, i){
legendData.push({sitename: myData[i][0], color: '#4ae412', hasIt: booleanData[i]});
});
console.log(legendData); // [{color: "#4ae412", hasIt: true, sitename: "site_1"}, {color: "#4ae412", hasIt: false, sitename: "site_2"}, {color: "#4ae412", hasIt: false, sitename: "site_2"}, {color: "#4ae412", hasIt: true, sitename: "site_4"}, {color: "#4ae412", hasIt: true, sitename: "site_5"}]
This has your color hardcoded as I am unaware of how you are figuring out the color. But you can simply do that calculation and store it as a variable and replace the hardcoded color with said variable.
Upvotes: 1
Reputation: 386560
You could map the wanted properties for a new object and get anarray of objects.
var myData = [["site_1", 3722, 2938, 2529, 3419, 3495, 3477], ["site_2", 115, 55, 56, 111, 113, 124], ["site_3", 13, 11, 10, 17, 17, 16], ["site_4", 16, 11, 10, 17, 17, 16], ["site_5", 0, 0, 0, 0, 0, 0]],
booleanData = [true, false, false, true, true],
result = myData.map(([siteName], i) => ({ siteName, color: "#4ae412", hasIt: booleanData[i] }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 2
Reputation: 746
Try the following usage of forEach, the callback can acept item
and index
as the second argument.
var myData = [["site_1", 3722, 2938, 2529, 3419, 3495, 3477],
["site_2", 115, 55, 56, 111, 113, 124],
["site_3", 13, 11, 10, 17, 17, 16],
["site_4", 16, 11, 10, 17, 17, 16],
["site_5", 0, 0, 0, 0, 0, 0]
];
var booleanData = [true, false, false, true, true];
var result = {};
myData.forEach((item, index) => {
let name = item[0]
result[name] = booleanData[index]
})
console.log(result)
// {site_1: true, site_2: false, site_3: false, site_4: true, site_5: true}
Upvotes: 2