javascript convert array of objects into similar but different structure - javascript

[NOTE: though similar to this stack question, my question differers significantly (IMO) due to the different structures of the initial variables. Mine is an array, the other questioner's is an object. My array contains nested objects/arrays, the latter of which contain name-value pairs whose values are, unlike the pairs in the other questioner's, not semantically identified.]
I have one big array (arr1) containing 24 objects. Each of the 24 objects contains one object, and one array of six further objects, like so:
var arr1 =
[
{'hour':1,'car':[{'audi':1377},{'bmw':716},{'ford':3819},{'mazda':67},{'toyota':11580},{'tesla':0}]},
{'hour':2,'car':[{'audi':1340},{'bmw':709},{'ford':3420},{'mazda':28},{'toyota':11583},{'tesla':0}]},
...etc, up to hour 24
];
As you can see, each of the 24 objects represents one hour's worth of data on car models and mileage. (What is NOT obvious is that the numeric value in each of the six objects represents miles.)
Now, I want to convert each object in arr1 into one array containing six objects, like below. So Hour 1's data in arr1 would convert to:
var arr2 = [{"car":"audi","miles":1377},{"car":"bmw","miles":716},{"car":"ford","miles":3819},{"car":"mazda","miles":67},{"car":"toyota","miles":11580},{"car":"tesla","miles":0}];
How can I do this? I have tried the following:
var hourx = 1;
var hour = arr1[hourx-1];
var car=hour.car;
for(var hourx1=0;hourx1<car.length;hourx1++){
var xx = car[hourx1];
var newobj = [];
for (var value in xx) {
var chartvar = newobj.push({car:value,miles:xx[value]});
var arr2 = newobj;
}
}
... but if I console.log(arr2); it only gives one array of one object.
I'm stumped. Anybody have an idea how I could accomplish this?

So you want to have an array of arrays as a final result?
var arr1 = [
{'hour':1,'car':[{'audi':1377},{'bmw':716},{'ford':3819},{'mazda':67},{'toyota':11580},{'tesla':0}]},
{'hour':2,'car':[{'audi':1340},{'bmw':709},{'ford':3420},{'mazda':28},{'toyota':11583},{'tesla':0}]}
];
var arr2 = arr1.map(function(hour){
return hour.car.map(function(car){
var carName = Object.keys(car)[0];
return {
"car": carName,
"miles": car[carName]
}
})
})
https://jsfiddle.net/1jjzeeyz/

Try using .map() , Object.keys()
var arr1 = [{
'hour': 1,
'car': [{
'audi': 1377
}, {
'bmw': 716
}, {
'ford': 3819
}, {
'mazda': 67
}, {
'toyota': 11580
}, {
'tesla': 0
}]
}, {
'hour': 2,
'car': [{
'audi': 1340
}, {
'bmw': 709
}, {
'ford': 3420
}, {
'mazda': 28
}, {
'toyota': 11583
}, {
'tesla': 0
}]
}];
var hours = arr1.map(function(cars, index) {
var carNames = Object.keys(cars.car);
var names = carNames.map(function(c) {
var name = Object.keys(cars.car[c])[0];
var res = {};
res["car"] = name;
res["miles"] = cars.car[c][name];
return res
})
return names
})
console.log(hours)

You would have to iterate over the array. Get the first property (let's hope it stays this way). Then get it value. Then create an object and add it to the array. See fiddle here
var arr1 =
[
{'hour':1,'car':[{'audi':1377},{'bmw':716},{'ford':3819},{'mazda':67},{'toyota':11580},{'tesla':0}]},
{'hour':2,'car':[{'audi':1340},{'bmw':709},{'ford':3420},{'mazda':28},{'toyota':11583},{'tesla':0}]},
];
var arr = [];
for (var i = 0; i< arr1[0].car.length; i++){
var car = arr1[0].car[i];
var key = Object.keys(car)[0];
arr.push({
car: key,
miles: car[key]
});
}

a combination of reduce and map will help. /example:
var
arr1 =[
{'hour':1,'car':[{'audi':1377},{'bmw':716},{'ford':3819},{'mazda':67},{'toyota':11580},{'tesla':0}]},
{'hour':2,'car':[{'audi':1340},{'bmw':709},{'ford':3420},{'mazda':28},{'toyota':11583},{'tesla':0}]}
],
arr2 = arr1.reduce(function (collector, item) {
collector.push(item.car.map(function (car/*, idx, list*/) {
var
make = Object.keys(car)[0]
;
return {
"car" : make,
"miles" : car[make]
}
}));
return collector;
}, [])
;
console.log("arr2 : ", arr2);

Related

Check if object has key, if so, add new key: value to said object

I have a loop that goes over an array of data plotData. Out of plotData I build another array called visiblePoints. Then I loop over visiblePoints to build an object thisRow, then I add thisRow to an array outside of the loop, called dataArray:
var dataArray = []
for (i = 0; i < plotData.length; i++) {
// This filters datapoints array into another array
var visiblePoints = filterPoints(plotData[i].datapoints)
// Get the string for the thisRow object key name
var selectedParameter = plotData[i].label
for (k = 0; k < visiblePoints.length; k++) {
// Convert visiblePoint first value from ms to date
var timestamp = new Date(visiblePoints[k][0])
// Form the thisRow object that will go into dataArray
// for each loop of visiblePoints
var thisRow = {}
thisRow["Time"] = msToTime(timestamp)
thisRow[selectedParameter] = visiblePoints[k][1]
dataArray.push(thisRow)
}
}
Let's simplify and say I only have 2 element in sub array visisblePoints for each plotData array, like so (each plotData array can have many visiblePoints but I'm simplifying to just 2 for each):
plotData[0].visiblePoints = [[00:00:01, 1], [00:00:02, 4] ...]
plotData[1].visiblePoints = [[00:00:01, 27], [00:00:02, 31] ...]
plotData looks like this on the console:
The visiblePoints array for the k loop is derived off of plotData.datapoints and looks like:
Where visiblePoints[n][0] is a value in ms (that I convert into seconds) and visiblePoints[n][1] is just a float value.
visiblePoints =
[0, 0.0500466109191]
[100, 0.0548114598135]
[200, 0.0550143573252]
[300, 0.0549408536766]
[400, 0.0546117305419]
[... repeat 300+ times...]
After looping over plotData[i] and visiblePoints[k] I end up with:
dataArray = [
{
"Time": 00:00:01,
"foo": 1
},
{
"Time": 00:00:01,
"bar": 27
},
{
"Time": 00:00:02,
"foo": 4
},
{
"Time": 00:00:02,
"bar": 31
},
]
When I had meant to end up with:
dataArray = [
{
"Time": 00:00:01,
"foo": 1,
"bar": 27
},
{
"Time": 00:00:02,
"foo": 4,
"bar": 31
}
]
I think in loop k I need to go over dataArray, check all objects there to see if there is a Time key that matches thisRow["Time"] = msToTime(timestamp), and if so, add thisRow[selectedParameter] = visiblePoints[k][1] to that, if not, create a new object.
Problems:
I'm not sure how to check for this in JS (I'm more experience at Python, and not that much at that
It seems like I'm doing a heck of a lot of loops. I'm not sure adding yet another one to go over the entire dataArray and check all objects to see if a key exists in one of them is best solution here. A plotData.visiblePoints array can be 500+ long.
[EDIT] Simplified the question. Added picture examples. Added text examples of the k array.
You can build an object keyed to the grouping — in this case the timestamp. This will let you randomly access the item you want without searching the array. When you'r done the Object.values of the object will be an array of your grouped objects:
let plotData = [
{
label: 'foo',
visiblePoints: [[`00:00:01`, 1], [`00:00:02`, 4]]
},
{
label: 'bar',
visiblePoints: [[`00:00:01`, 27], [`00:00:02`, 31]]
}
]
let groups = plotData.reduce((obj, {label, visiblePoints}) => {
visiblePoints.forEach(([time, val]) => {
if(!obj[time]) obj[time] = {Time: time} // if we haven't seen this time make a new object at that key
obj[time][label] = val
})
return obj
}, {})
console.log(Object.values(groups))
You could take a Map or a hash table which keeps the reference to the object with the same time.
var map = new Map,
timestamp,
row;
// later in loop
timestamp = msToTime(new Date(visiblePoints[k][0]));
row = map.get(timestamp);
if (!row) {
map.set(timestamp, row = { Time: timestamp });
}
row[selectedParameter] = visiblePoints[k][1];
// at the end assign the array
dataArray = Array.from(map.values())
You could find if an Object has a specific key by calling hasOwnProperty method.
const obj = {
"bar": [1,2,3],
"foo": "im foo"
};
console.log(obj.hasOwnProperty("bar"))
console.log(obj.hasOwnProperty("bar2"))
And you need to follow this pattern.
var dataArray = []
for (i = 0; i < 1; i++) {
for (k = 0; k < 2; k++) {
thisRow = {};
thisRow["Time"] = k+1 * i+1
thisRow["foo"] = i+1
thisRow["var"] = k+1
dataArray.push(thisRow)
}
}
console.log(dataArray)

Parse data into JavaScript arrays

I have an array data format coming from back-end which looks like:
Array
[{"ckey::"C1","date":"0506","rows":17},
{"ckey::"C1","date":"0706","rows":7},
{"ckey::"C2","date":"0706","rows":13},
{"ckey::"C2","date":"0806","rows":11}]
So for few days C1 data is there and few days C2 data.
Only one day has C1 and C2 data both.
I want to build an array like for C1 and C2
[[17,7,0],[0,13,11]]
First nested array for C1 where third value is 0 because for 0806 date the value was not present.
Second nested array for C2 where first value is 0 because for 0506 date the value was not present.
Please help. I cannot form the array effectively.
I think it would be O(n^3) solution. But please help with the same.
UPDATE
Here was my approach, I could not post the code here but it looks something like this.
I was getting date values in separate array like and I filter for unique days.
angular.forEach(data, function(obj){
if(timeData.indexOf(obj.date) === -1)
timeData.push(obj.date);
});
Then
ckey array _distinctckeyArray also were there containing values ["C1","C2"].
angular.forEach(_distinctckeyArray,function(_ckey){
var _formattedDataArrItem = [];
angular.forEach(timeData,function(_dateTimeString) {
var _tempDataVolume = [];
angular.forEach(_data,function(_dataObj) {
if(_dataObj.date === _dateTimeString) {
if(_dataObj.ckey === _ckey) {
_tempDataVolume.push(_dataObj.rows);
}else {
_tempDataVolume.push(0);
}
}
});
});
You can make an object dates that will have date properties. Initialize the values to 0
You reduce to group the array. Use Object.values to convert the object into an array.
let arr = [{ckey:"C1","date":"0506","rows":17},{ckey:"C1","date":"0706","rows":7},{ckey:"C2","date":"0706","rows":13},{ckey:"C2","date":"0806","rows":11}];
//Make an object with property dates. assign all property to 0 as initial value.
//Expected output:
//{"0506":0,"0706":0,"0806":0}
let dates = arr.reduce((c, v) => Object.assign(c, {[v.date]: 0}), {});
//Loop thru the array using `reduce`.
//This is to group the array to object using the ckey as the key
//After grouping, use `Object.values` to convert the object into array
let result = Object.values(arr.reduce((c, {ckey,date,rows}) => {
c[ckey] = c[ckey] || { ...dates }; //Check if c[ckey] exist. If it does not, "clone" the dates object.
c[ckey][date] = rows; //Overide the initial value 0 to the rows value
return c;
}, {})).map(o => Object.values(o));
console.log(result);
I think this is what you are looking for. Let me know.
let data = [{
'ckey': 'C1',
'date': '0506',
'rows': 17
}, {
'ckey': 'C1',
'date': '0706',
'rows': 7
}, {
'ckey': 'C2',
'date': '0706',
'rows': 13
}, {
'ckey': 'C2',
'date': '0806',
'rows': 11
}]
function nested_arrays(array) {
const result = []
const obj = {
c1: [],
c2: []
}
for (let i = 0; i < array.length; i++) {
if (array[i].ckey === 'C1') {
obj.c1.push(array[i].rows)
}
if (array[i].ckey === 'C2') {
obj.c2.push(array[i].rows)
}
}
obj.c1.push(0) // set last value to 0
obj.c2.unshift(0) // set first value to 0
result.push(obj.c1, obj.c2)
return result
}
let _tempDataVolume = nested_arrays(data)
console.log(_tempDataVolume) //=> [ [ 17, 7, 0 ], [ 0, 13, 11 ] ]
let arr = [{"ckey::"C1","date":"0506","rows":17},
{"ckey::"C1","date":"0706","rows":7},
{"ckey::"C2","date":"0706","rows":13},
{"ckey::"C2","date":"0806","rows":11}]
arr.map(res =>{
let c1arr = [],
let c2arr = [],
if(res.ckey== 'C1'){
c1arr.push(res.rows)
}else{ c2arr.push(res.rows) }
})
let newArrr = []
newArr.push(c1arr);
newArr.push(c2arr);
console.log('arr is',newArr)

Javascript two array merge( key and value)

I have two array. I want to merge this two arrays into one array. One array consisting keys and another one values.My array looks like
productId = [8,7,9];//Key Element
quantity = ["5","1","3"];//Value Element
//expected new array
newarray = {
"8": 5,
"7": 1,
"9": 3
}
I already tried to merge these arrays, in this way
var newArray = {};
for(var i=0; i< productId.length; i++){
newArray[productId[i]] = quantity [i];
}
console.log(newArray);
It returns
Object [ <7 empty slots>, "5", "1", "3" ]
You are working in firefox so you may get this type of issue because the problem might be caused at how Firefox' console.log has interpreted the input object.
Please look here
Empty slots in JavaScript objects?
Try this
var productId = [8,7,9];
var quantity = ["5","1","3"];
var newarray = {};
productId.forEach((key, i) => newarray[key] = quantity[i]);
console.log(newarray);
Try the following:
var productId = [8,7,9];//Key Element
var quantity = ["5","1","3"];//Value Element
var obj = {};
var i = 0;
for(var k of productId) {
obj[k] = parseInt(quantity[i]);
i++;
}
console.log(obj);
Your new "array" is not an Array but an Object.
You can iterate on one of the arrays using Array.reduce to construct the object.
Something like that:
const arr1 = ['8', '2', '4'];
const arr2 = ['28', '12', '45'];
const result = arr1.reduce((obj, currentItem, index) => {
obj[currentItem] = arr2[index];
return obj;
}, {});
console.log(result);

loop thought nested json array

I want to access salesId of json array but sales is an array also, so do loop twice?
var json = [
{
'id':1,
'sales':
[
{'salesId':123},
{'salesId':456}
]
},
{
'id':2,
'sales':
[
{'salesId':789},
{'salesId':111213}
]
}
];
for (var i in json) {
for (var j in json[i].sales) {
var result = json[i].sales[j].salesId; // here "result" will get salesId
}
}
See by yourself : here
How do you want the output?
json.map(function(x){ return x.sales.map(function(y){return y.salesId})})
returns ids per object
"[[123,456],[789,111213]]"
You can use inner loop instead, incase salesId is dynamic in sales.
for(var i=0;i<json.length;i++){
salesItem = json[i].sales;
for(var j=0;j<salesItem.length;j++){
var item = salesItem[j];
console.log(item.salesId);
}
}
If you don't care about the id you could simply flatten the array like so:
var newArray = json.reduce(function(p,c,i){
return i>1 ? p.concat(c.sales) : p.sales.concat(c.sales);
});
which will give you:
[ // newArray
{'salesId':123},
{'salesId':456},
{'salesId':789},
{'salesId':111213}
]
You could also use reduce to return just an array of salesId too if you wanted.
You don't need to loop twice
//loop through the json array that holds objects
for (var i=0; i<json.length; i++) {
var obj = json[i]; //reference to each object
var sales = obj.sales;
sales.forEach(function(element, index) {
console.log(element.salesId);
});
}
Here are two other ways. Not suggesting these are better, just 'other' ways.
var json = [
{
'id':1,
'sales':
[
{'salesId':123},
{'salesId':456}
]
},
{
'id':2,
'sales':
[
{'salesId':789},
{'salesId':111213}
]
}
];
one way:
var results = [];
for(i=0;i<json.length;i++){
results.push ( JSON.stringify(json[i].sales).match(/(\d+)/g,function($1){
return $1
}))
};
results; // [["123", "456"], ["789", "111213"]]
another way:
var str;
for(i=0;i<json.length;i++){
str = str + JSON.stringify(json[i].sales);
};
str = str.match(/(\d+)/g,function($1){
return $1
})
str; //["123", "456", "789", "111213"]

Combining results into one object

I'm looping through a set of inputs. I need to tally up the grouped totals. The inputs below to one of three categories.
How do I go about combining the values up relevant to three categories?
var compoundedArray = new Array();
holder.find(".dataset input").each(function(index) {
var val = $(this).val();
var dataType = $(this).data("type");
var localObj = {};
localObj[dataType] = val;
compoundedArray.push(localObj);
});
I have an object like this
[
{
"growth":30
},
{
"growth": 40
},
{
"other": 20
}
]
how do I loop through the object to produce something like
[
{
"growth": 70
},
{
"other": 20
}
]
if I looped over the initial array object
for (var i = 0; i < compoundedArray.length; i++) {
console.log(compoundedArray[i]);
}
how would I go about checking to ensure I don't have duplicates - and that I can tally up the results?
Ideally the resulting format may be the best
var array = [
"matching": 50,
"growth": 20
]
var array = [
"matching": 50,
"growth": 20
]
is not valid JS, but you can create an object of the form
var obj = {
"matching": 50,
"growth": 20
};
And that's pretty easy to do, just use an object from the very beginning:
var result = {};
holder.find(".dataset input").each(function(index) {
var val = +$(this).val(); // use unary plus to convert to number
var dataType = $(this).data("type");
result[dataType] = (result[dataType] || 0) + val;
});
Further reading material:
MDN - Working with Objects
Eloquent JavaScript - Data structures: Objects and Arrays
You can just use an object (not array) with unique keys.
var compoundedObj = {};
$(".dataset input", holder).each(function() {
var dataType = $(this).data("type");
if(!compoundedObj.hasOwnProperty(dataType)) {
compoundedObj[dataType] = 0;
}
compoundedObj[dataType] += parseInt($(this).val(), 10);
});
In this way you'll get an object like this:
{
"growth": 70,
"other": 20
}
Live demo
http://jsfiddle.net/GFwGU/
var original = [{"growth":30},{"growth": 40},{"other": 20}]
// object to sum all parts by key
var sums = {}
// loop through original object
for(var index in original){
// get reference to array value (target object)
var outer = original[index]
// loop through keys of target object
for(var key in outer){
// get a reference to the value
var value = outer[key]
// set or add to the value on the sums object
sums[key] = sums[key] ? sums[key] + value : value
}
}
// create the output array
var updated = []
// loop through all the summed keys
for(var key in sums){
// get reference to value
var value = sums[key]
// create empty object
var dummy = {}
// build object into desired format
dummy[key] = value
// push to output array
updated.push(dummy)
}
// check the results
alert(JSON.stringify( updated ))
var add=function (a,b){ a=a||0; b=b||0; return a+b};
var input=[ {growth:30},{growth:40},{other:20} ],output=[],temp={};
$.each(input,function(i,o){
var n;
for(i in o)
{n=i;break}
temp[n]=add(temp[n],o[n]);
});
$.each(temp,function(i,o){
var k={};
k[i]=o;
output.push(k)
});

Categories