Combine JavaScript array with multiple sub-values (Node.js / NVD3) - javascript

I'm trying to setup a Node.js API that sends JSON data to the client for use in an NVD3 chart. The chart accepts JSON input in the following format:
[
{
"key”:”KEY NAME“,
"values":[
[
1138683600000,
14.212410956029
],
[
1141102800000,
13.973193618249
]
]
},
{
"key”:”KEY NAME“,
"values":[
[
1138683600000,
7.1590087090398
],
[
1141102800000,
7.1297210970108
]
]
}
]
However, my Node program currently outputs JSON in this format:
[
{
"key”:”SAME KEY NAME”,
"values":[
1510148301000,
34
]
},
{
"key”:”SAME KEY NAME”,
"values":[
1509626301000,
55
]
},
{
"key”:”SAME KEY NAME“,
"values":[
1509539901000,
62
]
},
{
"key”:”DIFFERENT KEY NAME“,
"values":[
1509453501000,
58
]
}
]
I want to combine any "key" indices that are the same as other ones and merge the "values" with one another in the specified format. I searched all over to find a way to do this, but each method I came across didn't account for multiple pairings within the "value" index.
Any suggestions on how I could do this?
Thanks!

You can use Array.prototype.reduce to accumulate the items from your original array into an object keyed uniquely by the item's key-value. Since this leaves you with an Object instead of an array, you can then use Object.values to spit out the array of values like your example output.
let data = [
{"key":"A", "values":[1510148301000, 34]},
{"key":"A", "values":[1509626301000, 55]},
{"key":"A", "values":[1509539901000, 62]},
{"key":"B", "values":[1509453501000, 58]},
{"key":"B", "values":[1509453501001, 57]},
];
let combined = Object.values(data.reduce((accumulator, item) => {
if (!accumulator[item.key])
accumulator[item.key] = {key: item.key, values: []};
accumulator[item.key].values.push(item.values);
return accumulator;
}, {}));
console.log(combined);

I'm not sure about what you want (merge?), but it seems to be like that:
function combine (obj) {
var combined = {}
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (!!combined[key]) {
combined[key] = [].concat(combined[key], obj[key].values) // everything in one
// or
// combined[key].push(obj[key].values) // everything in distinct arrays
} else {
combined[key] = obj[key].values
// or
// combined[key] = [obj[key].values] // in distinct arrays
}
}
}
return combined
}

var original=[{"key":"SAME KEY NAME","values":[1510148301000,34]},{"key":"SAME KEY NAME","values":[1509626301000,55]},{"key":"SAME KEY NAME","values":[1509539901000,62]},{"key":"DIFFERENT KEY NAME","values":[1509453501000,58]}];
var result=[];
var isAlreadyAdded=false;
original.forEach(function(outerObj){
var newObj={};
var values=[];
original.forEach(function(element) {
if(newObj["key"] !== outerObj.key){
newObj["key"]=element.key;
values=[];
values.push(element["values"]);
}else if(outerObj.key ===element.key ){
values.push(element["values"]);
}
});
newObj["values"]=values;
var count=0;
result.push(newObj);
});
var temp=[];
result=result.filter((x, i)=> {
if (temp.indexOf(x.key) < 0) {
temp.push(x.key);
return true;
}
return false;
})
console.log(result);

Related

JavaScript: format CSV array to Json format

I have the following array which I have extracted from csv file
array
[
{ "Date;Department;Value": "2022-09-08;dept1;7856"},
{ "Date;Department;Value": "2022-09-08;dept2;9876"}
]
I need the following output:
[
{
"Date":"2022-09-08",
"Department":"dept1",
"Value":7856
},
{
"Date":"2022-09-08",
"Department":"dept2",
"Value":9876
}
]
That's quite doable, just take the problem step by step:
const array = [
{ "Date;Department;Value": "2022-09-08;dept1;7856" },
{ "Date;Department;Value": "2022-09-08;dept2;9876" }
];
const result = array
.map(Object.entries) // Convert the objects to more easily accessible arrays.
.map(([[k, v]]) => { // Grab the key / value strings
const keys = k.split(';'); // Split the keys into separate entries.
const values = v.split(';'); // Split the values into separate entries.
// Join the array of keys with the array of values, parsing numbers if possible.
return keys.reduce((obj, key, i) => {
obj[key] = isNaN(values[i]) ? values[i] : Number(values[i]);
return obj;
}, {});
});
console.log(result);

Filter an array of objects, by keys in filter object

i'm new here, i have problem that i can not solve.
I have 2 different arrays:
The first array - contains ratings of users with their ID name
[
{"handle":"frontend1", "_redis":"3", "_nodejs":"5", "_mysql":"2", "_python":"3", "_mongo":"4"},
{"handle":"frontend3", "_php":"4", "_mysql":"4", "_oracle":"4", "_ruby":"3", "_mongo":"5", "_python":"5"},
{"handle":"frontend4", "_java":"5", "_ruby":"5", "_mysql":"5", "_mongo":"5"}
]
The second set - contains the ratings, which I want to return to each user.
If there is a rating that is not in the second set, I will not return it
In the second set, values do not matter, only keys
[
"_assembler",
"_css",
"_python",
"_php"
]
I want to return to the first set, the handle, and all the rankings that exist in the second set.
[
{"handle":"frontend1", "_python":"3" },
{"handle":"frontend3", "_php":"4", "_python":"5" },
{"handle":"frontend4"}
]
this is what i try to do.
keys = [
"_assembler",
"_css",
"_python",
"_php"
]
source = [
{"handle":"frontend1", "_redis":"3", "_nodejs":"5", "_mysql":"2", "_python":"3", "_mongo":"4"},
{"handle":"frontend3", "_php":"4", "_mysql":"4", "_oracle":"4", "_ruby":"3", "_mongo":"5", "_python":"5"},
{"handle":"frontend4", "_java":"5", "_ruby":"5", "_mysql":"5", "_mongo":"5"}
];
result = [];
tmp = {};
source.forEach((item) => {
Object.keys(item).map(({key,value}) =>
{
if(key == "handle")
{
tmp[key]=value;
}
if(keys.includes(key))
{
tmp[key]=value;
}
})
result.push(...tmp);
tmp = {};
});
You can do this with a map utilizing a couple of other array methods such as filter, and Object methods.
const keys = [
"_assembler",
"_css",
"_python",
"_php"
]
const source = [
{"handle":"frontend1", "_redis":"3", "_nodejs":"5", "_mysql":"2", "_python":"3", "_mongo":"4"},
{"handle":"frontend3", "_php":"4", "_mysql":"4", "_oracle":"4", "_ruby":"3", "_mongo":"5", "_python":"5"},
{"handle":"frontend4", "_java":"5", "_ruby":"5", "_mysql":"5", "_mongo":"5"}
];
const result = source.map( s => ({
handle: s.handle,
...Object.fromEntries(Object.entries(s).filter(x => x[0] != "handle" && keys.includes(x[0])))
}));
console.log(result);

javascript recursive function concat not working

I have a nested array of objects like below and I'm trying to push all the values in to a single array. all the values are located in sp->it->value or sp->it->it->value
[
{
"sp": [
{
"it":[
{"value":5}
]
},
...
],
"b": {
...
}
},
{
"sp": [
{
"it":[
{"nm":5}
]
}
],
"b": {
...
}
},
{
"sp": [
{
"it":[
{
"it":[
{"value":5}
]
}
]
}
],
"b": {
...
}
},
]
and here is what I have tried
const getValues = (js) => {
let values = []
js.map((val,i) => {
if("sp" in val) values.concat(getValues(val.sp))
else if("it" in val) values.concat(getValues(val.it))
else if("value" in val) values.push(val.value)
})
return values
}
I thought I could concatenate the returned value from the recursive call since it returns an array but the above code returns empty array. Any insights?
Edit fixed the typo on sp object. It is array of objects.
Array.prototype.concat()
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
So those lines do nothing:
if("sp" in val) values.concat(getValues(val.sp))
else if("it" in val) values.concat(getValues(val.it))
You need to write:
if("sp" in val) values = values.concat(getValues(val.sp))
else if("it" in val) values = values.concat(getValues(val.it))
And you should not use map if you don't use it's result. Use forEach instead.
This is because you are passing val.sp to function which is not array but it is an object and .map is a property of an array

Creating a new key from child elements in Json with Javascript

I want to create new key from child values inside the json with ES6/ES5. I tried with arrow functions but i couldn't get the result. Firstly you can see my part of json in below,
[
{
matchId:307,
matchStatusId:5,
matchHomeScore:0,
matchAwayScore:0,
matchTime:0,
homeClubId:608,
homeClub:{
clubId:608,
clubName:"Annaba"
},
awayClubId:609,
awayClub:{
clubId:609,
clubName:"Bazer Sakhra"
},
leagues:[
{
leagueId:65,
parentLeagueId:null,
leagueName:"ALGERIA"
},
{
leagueId:66,
parentLeagueId:65,
leagueName:"Algeria Cup"
}
]
},
]
I want to create new parent key. It will get the values from values of child items and it will combine. Child item numbers changeable. Not everytime 2 items.
leaguesGeneral:"ALGERIA - Algeria Cup"
leaguesGeneral:"ALGERIA - Algeria Cup"
leagues: [
{
leagueId:65,
parentLeagueId:null,
leagueName:"ALGERIA"
},
{
leagueId:66,
parentLeagueId:65,
leagueName:"Algeria Cup"
}
]
I found this method. But it combines everything from the parent.
data = data.map(function (x) {
var keys = Object.keys(x);
x.newKeyValue = keys.map(key => x[key]).join('-');
return x;
});
The leaguesGeneral key can be added to each object in the array using
o.leagues.map(({ leagueName }) => leagueName).join(' - ')
const data = [
{
matchId:307,
matchStatusId:5,
matchHomeScore:0,
matchAwayScore:0,
matchTime:0,
homeClubId:608,
homeClub:{
clubId:608,
clubName:"Annaba"
},
awayClubId:609,
awayClub:{
clubId:609,
clubName:"Bazer Sakhra"
},
leagues:[
{
leagueId:65,
parentLeagueId:null,
leagueName:"ALGERIA"
},
{
leagueId:66,
parentLeagueId:65,
leagueName:"Algeria Cup"
}
]
},
];
const result = data.map(o => {
if (o.leagues) {
o.leaguesGeneral = o.leagues.map(({ leagueName }) => leagueName).join(' - ');
}
return o;
})
console.log(result)

How to merge items in an array by specific key?

I have a JavaScript object that has multiple attributes, and the value of these attributes is an array of items (type, count, senderUserName).
I want to combine the items in these arrays that have the same type while preserving the key that they originally correspond to.
Here is an example of my object with the arrays:
{C.J :[
{"type":"call","count":2,"senderUserName":"C.J.Rosati"},
{"type":"email","count":0,"senderUserName":"C.J.Rosati"},
{"type":"sms","count":1,"senderUserName":"C.J.Rosati"},
{"type":"sms","count":0,"senderUserName":"C.J.Rosati"},
{"type":"email","count":6,"senderUserName":"C.J.Rosati"}
],
Will :[
{"type":"call","count":2,"senderUserName":"Will"},
{"type":"email","count":1,"senderUserName":"Will"},
{"type":"sms","count":0,"senderUserName":"Will"},
{"type":"call","count":1,"senderUserName":"Will"},
{"type":"call","count":1,"senderUserName":"Will"},
]}
The result I want is:
{C.J :[
{"type":"call","count":2,"senderUserName":"C.J.Rosati"},
{"type":"email","count":6,"senderUserName":"C.J.Rosati"},
{"type":"sms","count":1,"senderUserName":"C.J.Rosati"}
],
Will :[
{"type":"call","count":4,"senderUserName":"Will"},
{"type":"email","count":1,"senderUserName":"Will"},
{"type":"sms","count":0,"senderUserName":"Will"},
]}
I have been playing around with lodash methods to get to my results but without any luck. I think I might have to use a combination of _.map and _.reduce but don't know how to combine them properly.
You can do this with reduce() and forEach() in pure javascript.
var data = {'C.J' :[
{"type":"call","count":2,"senderUserName":"C.J.Rosati"},
{"type":"email","count":0,"senderUserName":"C.J.Rosati"},
{"type":"sms","count":1,"senderUserName":"C.J.Rosati"},
{"type":"sms","count":0,"senderUserName":"C.J.Rosati"},
{"type":"email","count":6,"senderUserName":"C.J.Rosati"}
],
'Will' :[
{"type":"call","count":2,"senderUserName":"Will"},
{"type":"email","count":1,"senderUserName":"Will"},
{"type":"sms","count":0,"senderUserName":"Will"},
{"type":"call","count":1,"senderUserName":"Will"},
{"type":"call","count":1,"senderUserName":"Will"},
]}
result = Object.keys(data).reduce(function(r, e) {
var ar = [];
data[e].forEach(function(a) {
if (!this[a.type]) {
this[a.type] = a;
ar.push(this[a.type])
} else {
this[a.type].count += a.count;
}
}, {})
r[e] = ar;
return r;
}, {})
console.log(result)
That is, of course, if you have ES6 available and aren't trying to run this client-side in older browsers.
If you're set on lodash -- you can do something like this:
var data = {"C.J" :[
{"type":"call","count":2,"senderUserName":"C.J.Rosati"},
{"type":"email","count":0,"senderUserName":"C.J.Rosati"},
{"type":"sms","count":1,"senderUserName":"C.J.Rosati"},
{"type":"sms","count":0,"senderUserName":"C.J.Rosati"},
{"type":"email","count":6,"senderUserName":"C.J.Rosati"}
],
"Will" :[
{"type":"call","count":2,"senderUserName":"Will"},
{"type":"email","count":1,"senderUserName":"Will"},
{"type":"sms","count":0,"senderUserName":"Will"},
{"type":"call","count":1,"senderUserName":"Will"},
{"type":"call","count":1,"senderUserName":"Will"},
]}
var combined_data = _.map(data, function(element, key){
var collapsed_element = [];
var element_type_hash = {};
_.each(element, function(record){
if(element_type_hash[record.type] !== undefined){
element_type_hash[record.type].count += record.count;
} else
{
element_type_hash[record.type] = record;
}
});
_.each(element_type_hash, function(element_obj){
collapsed_element.push(element_obj);
});
return collapsed_element;
});
console.log(JSON.stringify(combined_data));
https://jsfiddle.net/oumba4p5/1/
You can use _.transform() to collect the keys into a dictionary, and extract the combined values using _.values():
var data = {
'C.J' :[
{"type":"call","count":2,"senderUserName":"C.J.Rosati"},
{"type":"email","count":0,"senderUserName":"C.J.Rosati"},
{"type":"sms","count":1,"senderUserName":"C.J.Rosati"},
{"type":"sms","count":0,"senderUserName":"C.J.Rosati"},
{"type":"email","count":6,"senderUserName":"C.J.Rosati"}
],
'Will' :[
{"type":"call","count":2,"senderUserName":"Will"},
{"type":"email","count":1,"senderUserName":"Will"},
{"type":"sms","count":0,"senderUserName":"Will"},
{"type":"call","count":1,"senderUserName":"Will"},
{"type":"call","count":1,"senderUserName":"Will"},
]
};
var result = _.transform(data, function(result, value, key) { // transform the original object
result[key] = _(value)
.transform(function(merged, obj) { // transform the array of objects
if(merged[obj.type]) { // if the type key already exists
merged[obj.type].count += obj.count; // add to count
} else { // if not
merged[obj.type] = obj; // the obj will be used for the type property
}
})
.values() // take just the values
.value();
});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

Categories