Merge JavaScript objects with the same key value and count them - javascript

I'm trying to merge objects with the same key value into one and count them. Is it even possible?
var array = {
"items": [{
"value": 10,
"id": "111",
"name": "BlackCat",
}, {
"value": 10,
"id": "111",
"name": "BlackCat",
}, {
"value": 15,
"id": "777",
"name": "WhiteCat",
}]
}
Desired output:
var finalArray = {
"items": [{
"value": 10,
"id": "111",
"name": "BlackCat",
"count": 2,
}, {
"value": 15,
"id": "777",
"name": "WhiteCat",
"count": 1,
}]
}

You can use reduce on your items array:
var combinedItems = array.items.reduce(function(arr, item) {
var found = false;
for (var i = 0; i < arr.length; i++) {
if (arr[i].id === item.id) {
found = true;
arr[i].count++;
}
}
if (!found) {
item.count = 1;
arr.push(item);
}
return arr;
}, [])
Fiddle: https://jsfiddle.net/6wqw79pn/

You could use a for loop:
var finalArray = [];
for (var i in array.items) {
var item = array.items[i];
var existingItem = finalArray.find(function(element){
return (element.id == item.id);
});
if (!existingItem) {
existingItem = {};
finalArray.push(existingItem);
} else {
if (!existingItem.count)
existingItem.count = 2;
else
existingItem.count++;
}
for (var j in item) {
existingItem[j] = item[j];
}
}
Working example: https://jsfiddle.net/mspinks/5kowbq4j/3/

Basically you could use a hash table with the value of id as key and count.
var object = { items: [{ value: 10, id: "111", name: "BlackCat", }, { value: 10, id: "111", name: "BlackCat", }, { value: 15, id: "777", name: "WhiteCat", }] },
result = object.items.reduce(function (hash) {
return function (r, o) {
if (!hash[o.id]) {
hash[o.id] = { value: o.value, id: o.id, name: o.name, count: 0 };
r.push(hash[o.id]);
}
hash[o.id].count++;
return r;
};
}(Object.create(null)), []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Underscore js will be able to solve your problem very easily
var groups = _.groupBy(array.items, function(item){
return item.value + '#' + item.id + '#' + item.name;
});
var data = {'items' : []};
_.each(groups,function(group){
data.items.push({
'value' : group[0].value,
'id' : group[0].id,
'name' : group[0].name,
'count' : group.length
})
})
console.log(data)

Related

Delete main array when nested array is empty

I got the following array:
var arr = [{
"mainId": 1,
"parents": [{
"parent": 1
},
{
"parent": 2
}
]
},
{
"mainId": 2,
"parents": [{
"parent": 3
}]
}
]
I'm using this function to delete an specific parent in the parents array
var idToDelete = 2
arr.forEach(function (o) {
o.parents = o.parents.filter(s => s.id !== idToDelete
})
This is working fine. But when idToDelete = 3 I want to delete the complete main Item and start a function "startSomething"
So that I'm left with the following output
var arr = [{
"mainId": 1,
"parents": [{
"parent": 1
},
{
"parent": 2
}
]
}]
How could this be implemented?
You can map and then filter the top level array, here is an example:
var arr = [{
"mainId": 1,
"parents": [{
"parent": 1
},
{
"parent": 2
}
]
},
{
"mainId": 2,
"parents": [{
"parent": 3
}]
}
];
var idToDelete = 3;
arr = arr.map((v) => ({
...v,
parents: v.parents.filter(({
parent
}) => parent !== idToDelete)
}))
.filter((v) => v.parents.length);
console.log(arr);
Filter the parent array by whether it has a .length after mutating the children. Also make sure to use the .parent property (your objects have no .id property):
const startSomething = () => console.log('startSomething');
var arr =
[
{
"mainId": 1,
"parents": [
{
"parent": 1
},
{
"parent": 2
}
]
},
{
"mainId": 2,
"parents": [
{
"parent": 3
}
]
}
];
var idToDelete = 3;
arr.forEach(function (o) {
o.parents = o.parents.filter(s => s.parent !== idToDelete)
});
const newArr = arr.filter(({ parents }) => parents.length);
console.log(newArr);
if (newArr.length !== arr.length) {
startSomething();
}
You can use .filter() on the array itself (to remove the main object) with .some() to return true or false depending on whether it should be deleted or not:
const arr = [{ "mainId": 1, "parents": [{ "parent": 1 }, { "parent": 2 } ] }, { "mainId": 2, "parents": [{ "parent": 3 }] } ];
const removeObj = (arr, id, cb) => {
const res = arr.filter(({parents}) => !parents.some(({parent}) => parent === id));
const item_removed = res.length !== arr.length;
return (item_removed && cb(res), item_removed);
}
const startSomething = new_arr => console.log("Starting with arr", new_arr);
removeObj(arr, 3, startSomething);
.as-console-wrapper { max-height: 100% !important;} /* ignore */
Here's a alternative method, with a function that uses findIndex to find the object in the array.
Then splices the object if it only has 1 parent.
Or splices the parent from object if it has more than 1 parent.
Example snippet:
const deleteParent = (arr, id) =>
{
let idx = arr.findIndex(x=>x.parents.some(p=> p.parent === id));
if (idx >= 0 && arr[idx].parents.length === 1 ) {
let obj = arr[idx];
// remove object from array and do something
arr.splice(idx, 1);
doSomething(obj);
}
else if(idx >= 0){
let obj = arr[idx];
let parentIdx = obj.parents.findIndex(x=>x.parent==id);
// remove parent
obj.parents.splice( parentIdx, 1);
}
}
function doSomething (obj) { console.log(`Deleted mainId ${obj.mainId}`) }
var arr = [
{
"mainId": 1,
"parents": [
{
"parent": 1
},
{
"parent": 2
}
]
},
{
"mainId": 2,
"parents": [
{
"parent": 3
}
]
}
];
deleteParent(arr, 3);
console.log(JSON.stringify(arr));
deleteParent(arr, 2);
console.log(JSON.stringify(arr));

How to reduce an array while merging one of it's field as well

I have this,
var o = [{
"id": 1, // its actually a string in real life
"course": "name1",
// more properties
},
{
"id": 1, // its actually a string in real life
"course": "name2",
// more properties
}];
I want this,
var r = [{
"id": 1, // its actually a string in real life
"course": ["name1", "name2"],
}];
I am trying this,
var flattened = [];
for (var i = 0; i < a.length; ++i) {
var current = a[i];
if(flattened.)
}
but I am stuck, I am not sure what to do next, array will have more then 2 records but this was just an example.
THERE are more fields but I removed them for simplicity, I won't be using them in final array.
You could reduce the array and find the object.
var array = [{ id: 1, course: "name1" }, { id: 1, course: "name2" }],
flat = array.reduce((r, { id, course }) => {
var temp = r.find(o => id === o.id);
if (!temp) {
r.push(temp = { id, course: [] });
}
temp.course.push(course);
return r;
}, []);
console.log(flat);
The same by taking a Map.
var array = [{ id: 1, course: "name1" }, { id: 1, course: "name2" }],
flat = Array.from(
array.reduce((m, { id, course }) => m.set(id, [...(m.get(id) || []) , course]), new Map),
([id, course]) => ({ id, course })
);
console.log(flat);
This way you will get the data flattened in the shape you want
const o = [
{
id: 1,
course: "name1"
},
{
id: 1,
course: "name2"
},
{
id: 2,
course: "name2"
}
];
const r = o.reduce((acc, current) => {
const index = acc.findIndex(x => x.id === current.id);
if (index !== -1) {
acc[index].course.push(current.course);
} else {
acc.push({id:current.id, course: [current.course]});
}
return acc
}, []);
console.log(r);
You can do this with reduce and Object.entries. This example works for any number of properties:
const o = [
{ id: 1, course: 'name1', time: 'morning', topic: 'math' },
{ id: 1, course: 'name2', time: 'afternoon' },
{ id: 2, course: 'name3', time: 'evening' }
];
const result = o.reduce((out, { id, ...rest }) => {
out[id] = out[id] || {};
const mergedProps = Object.entries(rest).reduce((acc, [k, v]) => {
return { ...acc, [k]: [...(out[id][k] || []), v] };
}, out[id]);
out[id] = { id, ...mergedProps };
return out;
}, {});
console.log(result);
If you only care about the id and course fields, you can simplify to this:
const o = [
{ id: 1, course: 'name1', time: 'morning', topic: 'math' },
{ id: 1, course: 'name2', time: 'afternoon' },
{ id: 2, course: 'name3', time: 'evening' }
];
const result = o.reduce((out, { id, course }) =>
({ ...out, [id]: { id, course: [...((out[id] || {}).course || []), course] } })
, {});
console.log(result);
You could use .reduce to create an object of keys, and then use that object to set keys to be of the id. This way you can add to the same course array by targetting the id of the object. Lastly, you can get the values of the object to get your result.
See example below:
var o = [{
"id": 1,
"course": "name1",
"foo": 1
},
{
"id": 1,
"course": "name2",
"bar": 2
}];
var res = Object.values(o.reduce((acc, {id, course, ...rest}) => {
if(id in acc)
acc[id] = {...acc[id], course: [...acc[id].course, course], ...rest};
else acc[id] = {id, course: [course], ...rest};
return acc;
}, {}));
console.log(res);
function merge(array, key = 'id') {
const obj = {}
for(const item of array) {
const existing = obj[item[key]]
if(existing) {
for(const [name, value] of Object.entries(item)) {
if(name === key) continue;
if(existing[name]) {
existing[name] = [ ...(existing[name].$custom ? existing[name] : [existing[name]]), value ]
existing[name].$custom = true;
} else {
existing[name] = value;
}
}
} else {
obj[item[key]] = { ...item }
}
}
return Object.values(obj)
}
var o = [
{
"id": 1,
"single": "test"
},
{
"id": 1,
"course": "name1",
"multifield": "test"
},
{
"id": 1,
"course": "name2"
},
{
"id": 1,
"newfield": "test"
}, {
"id": 2,
"anotherid": "test",
"array": [1,3,4]
}, {
"id": 2,
"array": "text"
}];
console.log(merge(o))
You can use reduce to accumulate the results. Search in the current result (accumulator a) for an object (el) with the same id, if found, append course to existing object and return the same accumulator, otherwise put into the accumulator with course as an array.
var res = o.reduce((a, {id,course}) => {
var found = a.find(el => el.id == id);
return found ? found.course.push(course) && a : [...a, {id, course: [course]}];
}, []);

Iterate through an array of objects, to find objects that contain 2 values

I currently have an array that looks like this:
var array = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "b",
"age": 3,
"siblings": 5
},
{
"name": "a",
"age": 1,
"siblings": 2
}
]
I want to create a function that takes 2 values, and returns a new array containing the objects that match those two values.
var name = "a";
var age = 1;
someFunction(name, age);
And returns something that looks like this:
newArray = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "a",
"age": 1,
"siblings": 2
}
]
I have tried using the filter method and the reduce methods but no success. If someone could help me or point me in the right direction, I would greatly appreciate that.
var array = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "b",
"age": 3,
"siblings": 5
},
{
"name": "a",
"age": 1,
"siblings": 2
}
]
var result = array.filter(function(obj) {
return obj.name === "a" && obj.age === 1;
});
return result[0];
}
console.log(result);
You could take an array with the wanted key/value pair for searching.
Take Array#filter with a check for the predicates with Array#every.
var array = [{ name: "a", age: 1, siblings: 3 }, { name: "b", age: 3, siblings: 5 }, { name: "a", age: 1, siblings: 2 }],
search = [['name', 'a'], ['age', 1]],
result = array.filter(object => search.every(([key, value]) => object[key] === value));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
here is your working function
function someFunction (name, age) {
return array.filter(val => val.name == name && val.age == age)
}
In the Array#filter callback, just compare the name & age of current element with those of the params you passed.
var array = [{"name":"a","age":1,"siblings":3},{"name":"b","age":3,"siblings":5},{"name":"a","age":1,"siblings":2}]
var name = "a";
var age = 1;
function someFunction(name, age){
return array.filter((obj) => obj.name === name && obj.age === age)
}
console.log(someFunction(name, age));
You can also use Array#reduce. Just add the matching object in the accumulator array and return it.
var array = [{"name":"a","age":1,"siblings":3},{"name":"b","age":3,"siblings":5},{"name":"a","age":1,"siblings":2}]
var name = "a";
var age = 1;
function someFunction(name, age){
return array.reduce((acc, obj) => {
if(obj.name === name && obj.age === age)
acc.push(obj);
return acc;
}, []);
}
console.log(someFunction(name, age));
Use the filter function
var array = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "b",
"age": 3,
"siblings": 5
},
{
"name": "a",
"age": 1,
"siblings": 2
}
]
var name = "a";
var age = 1;
someFunction(name, age);
function someFunction(name,age)
{
console.log(array.filter((e)=>e.name==name && e.age==age?true:false
))}
Try this
let array = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "b",
"age": 3,
"siblings": 5
},
{
"name": "a",
"age": 1,
"siblings": 2
}
];
function someFunction(name, age) {
let newArray = [];
array.forEach(function(arr) {
let nameMatch = false;
let ageMatch = false;
let obj = arr;
for(key in obj) {
if(obj.hasOwnProperty(key)) {
if(key === 'name') {
// console.log('name key i am', key);
let nameValue = obj[key];
if(nameValue === name) {
nameMatch = true;
// console.log('name match', nameValue, name);
}
} else if(key === 'age') {
let ageValue = obj[key];
if(ageValue === age) {
// console.log('age match', ageValue, age);
ageMatch = true
}
}
}
}
if(ageMatch && nameMatch) {
// console.log('true');
newArray.push(arr);
// console.log(arr);
}
});
return newArray;
}
let name = "a";
let age = 1;
let myArr = someFunction(name, age);
console.log('My Arr', myArr);

How to give index of array in attribute while creating object

I am creating an object in javascript a which is based on some array data
var myArr = [
{
"id": 1,
"Music": "POP",
"Singer": "Abc",
"Country": "IND"
},
{
"id": 2,
"Music": "JAZZ",
"Singer": "xyz",
"Country": "USA"
},
{
"id": 3,
"Music": "BLUES",
"Singer": "def",
"Country": "ENG"
}
]
Now Based on this array I am trying to create an object of array which having
id, Value and Country.
Here What I am trying
var myObj = [];
for (var i = 0; i < myArr.length; i++) {
var obj = {
id: i,
Value: myArr[i].Music,
myArr[i].Singer : myArr[i].Country,
}
myObj.push(obj);
}
But during this for loop myArr[i].Singer is throwing an error. How to resolve this.
With an array, take the wanted key and assign the value directly outside of the object literal.
var myArr = [{ id: 1, Music: "POP", Singer: "Abc", Country: "IND" }, { id: 2, Music: "JAZZ", Singer: "xyz", Country: "USA" }, { id: 3, Music: "BLUES", Singer: "def", Country: "ENG" }],
myObj = [],
obj;
for (var i = 0; i < myArr.length; i++) {
obj = {
id: i,
Value: myArr[i].Music
}
obj[myArr[i].Singer] = myArr[i].Country;
myObj.push(obj);
}
console.log(myObj);
.as-console-wrapper { max-height: 100% !important; top: 0; }
With ES6 you could use computed property names and map the object for a new array.
var array = [{ id: 1, Music: "POP", Singer: "Abc", Country: "IND" }, { id: 2, Music: "JAZZ", Singer: "xyz", Country: "USA" }, { id: 3, Music: "BLUES", Singer: "def", Country: "ENG" }],
result = array.map((o, i) => ({ id: i, Value: o.Music, [o.Singer]: o.Country}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var myObj = [];
for(var item in myArr){
if(item.hasOwnProperty(“Music”){
myObj .push(item.Music);
}
}
You can use array#map.
var myArr = [{"id":1,"Music":"POP","Singer":"Abc","Country": "IND"},{"id":2,"Music":"JAZZ","Singer":"xyz","Country": "USA"},{"id":3,"Music":"BLUES","Singer":"def","Country": "ENG"}];
var result = myArr.map(({Music, Singer, Country}, i) => ({id : i, Value: Music, [Singer] : Country}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Here is a little bit of ES6/ES7. Create a new array of objects with the desired props from the existing array of objects.
const modifiedArray = myArr.map( item => {
//This will return only the elements from the object you want
const picked = (({
width,
height,
type,
size,
name
}) => ({
width,
height,
type,
size,
name
}))(item);
// Return the object and insert it in the array
return {
...picked,
};
});
You can use Array.prototype.map():
var myArr = [{"id": 1,"Music": "POP","Singer": "Abc","Country": "IND"}, {"id": 2,"Music": "JAZZ","Singer": "xyz","Country": "USA"}, {"id": 3,"Music": "BLUES","Singer": "def","Country": "ENG"}],
myObj = myArr.map(function (elem, index) {
return {
id: index,
Value: elem.Music,
[elem.Singer]: elem.Country
};
});
console.log(myObj);

Merge json object and create new object, inside json object using javascript

My JSON looks like this
{"rows":[
{"shiftId":1,"shift":"Morning","item":"Tea","value":20},
{"shiftId":1,"shift":"Morning","item":"Coffee","value":30},
{"shiftId":2,"shift":"Evening","item":"Tea","value":40},
{"shiftId":2,"shift":"Evening","item":"Coffee","value":35}
]}
and I am looking to merge all the same shift and add the values of the merged keys together and create new Object for item to get something looking like this
{"rows":[
{
"shiftId":1,
"shift":"Morning",
"item":[{"itemName":"Tea"},{"itemName":"Coffee"}],
"value":50
},
{
"shiftId":2,
"shift":"Evening",
"item":[{"itemName":"Tea"},{"itemName":"Coffee"}],
"value":75
}
]}
I am try like this
var merged = {rows: []};
data.forEach(function (source) {
if (!merged.rows.some(function (row) {
return row.shiftId == source.shiftId;
})) {
merged.rows.push({
shiftId: source.shift,
shift: source.shift,
item: [{
itemName: source.shift
}],
value: source.value
});
} else {
var existRow = merged.rows.filter(function (existRow) {
return existRow.shiftId == source.shiftId
})[0];
existRow.total += source.total;
existRow.item = source.item.push(existRow.item);
}
});
But not working correctly. Thanks advance.
You could use a hash table as a reference to the objects with the same shiftId and return a new array with the collected and grouped data.
var data = { rows: [{ shiftId: 1, shift: "Morning", item: "Tea", value: 20 }, { shiftId: 1, shift: "Morning", item: "Coffee", value: 30 }, { shiftId: 2, shift: "Evening", item: "Tea", value: 40 }, { shiftId: 2, shift: "Evening", item: "Coffee", value: 35 }] },
result = {
rows: data.rows.reduce(function (hash) {
return function (r, a) {
if (!hash[a.shiftId]) {
hash[a.shiftId] = { shiftId: a.shiftId, shift: a.shift, item: [], value: 0 };
r.push(hash[a.shiftId]);
}
hash[a.shiftId].item.push({ itemName: a.item });
hash[a.shiftId].value += a.value;
return r;
};
}(Object.create(null)), [])
};
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Use a hash table:
var hash={};
var input={"rows":[
{"shiftId":1,"shift":"Morning","item":"Tea","value":20},
{"shiftId":1,"shift":"Morning","item":"Coffee","value":30},
{"shiftId":2,"shift":"Evening","item":"Tea","value":40},
{"shiftId":2,"shift":"Evening","item":"Coffee","value":35}
]}.rows;
input.forEach(function(row){
var el=(hash[row.shiftId]=hash[rows.shiftId]||{shiftId:row.shiftId,shift:row.shift,items:[],value:0});
el.items.push({itemName:row.itemName});
el.value+=row.value;
});
Now you can create your result like this:
var result={rows:[]};
for(key in hash){
result.rows.push(hash[key]);
}
Using Array.prototype.reduce()
const arr = [{
"shiftId": 1,
"shift": "Morning",
"item": "Tea",
"value": 20
},
{
"shiftId": 1,
"shift": "Morning",
"item": "Coffee",
"value": 30
},
{
"shiftId": 2,
"shift": "Evening",
"item": "Tea",
"value": 40
},
{
"shiftId": 2,
"shift": "Evening",
"item": "Coffee",
"value": 35
}
];
const newArr = arr.reduce((acc, item, i) => {
if (!acc.length) { // First time
acc.push(item)
return acc;
}
if (acc[acc.length - 1].shiftId === item.shiftId) { // if current shiftId === last shiftId
if (!(acc[acc.length - 1].item instanceof Array)) {
acc[acc.length - 1].item = [{ // Convert item to be an array
"itemName": acc[acc.length - 1].item
}]
}
acc[acc.length - 1].item.push({ // Add current item name to the last item array
"itemName": item.item
});
acc[acc.length - 1].value = acc[acc.length - 1].value + item.value // value addition
} else { // If new shiftId
acc.push(item);
}
return acc;
}, []);
console.log(newArr);
a = {"rows":[
{"shiftId":1,"shift":"Morning","item":"Tea","value":20},
{"shiftId":1,"shift":"Morning","item":"Coffee","value":30},
{"shiftId":2,"shift":"Evening","item":"Tea","value":40},
{"shiftId":2,"shift":"Evening","item":"Coffee","value":35}
]};
newa = {"rows":[]};
lastshiftid = -1;
newkey = -1;
for(key in a.rows){
curshiftid = a.rows[key].shiftId;
o = {"item":a.rows[key].item};
if(lastshiftid!=curshiftid){
newkey++;
a.rows[key].item = [];
newa.rows.push(a.rows[key]);
}
newa.rows[newkey].item.push(o);
lastshiftid = curshiftid;
}
console.log(newa.rows);
Fiddle: https://jsfiddle.net/t74ygy9L/

Categories