JavaScript remove duplicate object and access data from removed object and append - javascript

var array = [{
id: "decafc0ffeefacedbabef00ddeadbeef",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef"
}, {
id: "4bb6ac319db42fabab84826a1c08e8da",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd;;4bb6ac319db42fabab84826a1c08e8da"
}, {
id: "4bb6ac319db42fabab84826a1c08e8da",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0;;4bb6ac319db42fabab84826a1c08e8da"
}, {
id: "47421d5c40b2f15d801ac6ca0ff4e6cd",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd"
}, {
id: "4ace8bd1ec354275a813d6e3725047c0",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0"
}];
var keyToBeUnique = 'id';
var newarray = array.filter((val, key) => {
return !array.slice(key + 1)
.some((valNew) => {
if(valNew[keyToBeUnique] === val[keyToBeUnique])
valNew['long_id'] = val['long_id'] +','+ valNew['long_id'];
return valNew[keyToBeUnique] === val[keyToBeUnique];
})
});
console.log(newarray);
Looking for a better way to append the long_Id "valNew['long_id'] = val['long_id'] +','+ valNew['long_id'];" which is written inside "some" function

you can get your result using reduce, it is simplier to what you want to achieve.
var array = [{
id: "decafc0ffeefacedbabef00ddeadbeef",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef"
}, {
id: "4bb6ac319db42fabab84826a1c08e8da",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd;;4bb6ac319db42fabab84826a1c08e8da"
}, {
id: "4bb6ac319db42fabab84826a1c08e8da",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0;;4bb6ac319db42fabab84826a1c08e8da"
}, {
id: "47421d5c40b2f15d801ac6ca0ff4e6cd",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd"
}, {
id: "4ace8bd1ec354275a813d6e3725047c0",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0"
}];
function reducer(array, keyToBeUnique) {
return array.reduce((accum, cv) => {
const index = accum.findIndex(item => item[keyToBeUnique] === cv[keyToBeUnique])
// if the index is -1 it means you dont have that ID yet, then push it.
if (index === -1) {
accum.push(cv)
} else {
// if it is not -1 you can edit the long_id property and add your strings.
accum[index]['long_id'] = accum[index]['long_id'] + ', ' + cv['long_id'];
}
return accum;
}, []);
}
console.log(reducer(array, 'id'));

you can use .map() to extract the ids and a new Set() to remove the duplicates, then remap the resulting array to put back the elements from the original one :
var array = [{
id: "decafc0ffeefacedbabef00ddeadbeef",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef"
}, {
id: "4bb6ac319db42fabab84826a1c08e8da",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd;;4bb6ac319db42fabab84826a1c08e8da"
}, {
id: "4bb6ac319db42fabab84826a1c08e8da",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0;;4bb6ac319db42fabab84826a1c08e8da"
}, {
id: "47421d5c40b2f15d801ac6ca0ff4e6cd",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd"
}, {
id: "4ace8bd1ec354275a813d6e3725047c0",
long_id: "1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0"
}];
const filterArray = (array, key) => {
const deduped = [...new Set(array.map(e => e[key]))];
const arr = deduped.map(el => {
const ndx = array.findIndex(e => e[key] === el);
return {
[key]: array[ndx][key],
long_id: array.filter(e => e[key] === el).map(e => e.long_id).join(';;')
}
});
return arr;
}
console.log(filterArray(array, 'id'))

As suggested here use an array, hope it helps :)
var array = [{
id: "decafc0ffeefacedbabef00ddeadbeef",
long_id: ["1;;decafc0ffeefacedbabef00ddeadbeef"]
}, {
id: "4bb6ac319db42fabab84826a1c08e8da",
long_id: ["1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd;;4bb6ac319db42fabab84826a1c08e8da"]
}, {
id: "4bb6ac319db42fabab84826a1c08e8da",
long_id: ["1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0;;4bb6ac319db42fabab84826a1c08e8da"]
}, {
id: "47421d5c40b2f15d801ac6ca0ff4e6cd",
long_id: ["1;;decafc0ffeefacedbabef00ddeadbeef;;47421d5c40b2f15d801ac6ca0ff4e6cd"]
}, {
id: "4ace8bd1ec354275a813d6e3725047c0",
long_id: ["1;;decafc0ffeefacedbabef00ddeadbeef;;4ace8bd1ec354275a813d6e3725047c0"]
}];
var output = [];
array.forEach(function(item) {
var existing = output.filter(function(v, i) {
return v.id == item.id;
});
if (existing.length) {
var existingIndex = output.indexOf(existing[0]);
output[existingIndex].long_id = output[existingIndex].long_id.concat(item.long_id);
} else {
if (typeof item.value == 'string')
item.long_id = [item.long_id];
output.push(item);
}
});
console.dir(output);

Related

Return similar values ​from multiple array of objects in Javascript

Guys I made a simple example to illustrate my problem. I have 3 object arrays, datasOne, datasTwo and datasThree and what I want is to return a new array only with the objects that are in the 3 arrays. For example, if there is only Gustavo in the 3 arrays, then he will be returned. But there is a detail that if the datasThree is an empty array, then it will bring the data in common only from datasOne and datasTwo and if only the datasTwo which has data and the other two arrays have empty, then it will return data only from datasTwo. In other words it is to return similar data only from arrays that have data. I managed to do this algorithm and it works the way I want, but I would like to know another way to make it less verbose and maybe simpler and also work in case I add more arrays to compare like a dataFour for example. I appreciate anyone who can help me.
My code below:
let datasOne = [
{ id: 1, name: 'Gustavo' },
{ id: 2, name: 'Ana' },
{ id: 3, name: 'Luiz' },
{ id: 8, name: 'Alice' }
]
let datasTwo = [
{ id: 1, name: 'Gustavo' },
{ id: 3, name: 'Luiz' },
{ id: 8, name: 'Alice' }
]
let datasThree = [
{ id: 1, name: 'Gustavo' },
{ id: 3, name: 'Luiz' },
{ id: 2, name: 'Ana' },
{ id: 5, name: 'Kelly' },
{ id: 4, name: 'David' }
]
let filtered
if (datasOne.length > 0 && datasTwo.length > 0 && datasThree.length > 0) {
filtered = datasOne.filter(firstData => {
let f1 = datasThree.filter(
secondData => firstData.id === secondData.id
).length
let f2 = datasTwo.filter(
secondData => firstData.id === secondData.id
).length
if (f1 && f2) {
return true
}
})
} else if (datasOne.length > 0 && datasTwo.length > 0) {
filtered = datasOne.filter(firstData => {
return datasTwo.filter(secondData => firstData.id === secondData.id).length
})
} else if (datasOne.length > 0 && datasThree.length > 0) {
filtered = datasOne.filter(firstData => {
return datasThree.filter(secondData => firstData.id === secondData.id)
.length
})
} else if (datasTwo.length > 0 && datasThree.length > 0) {
filtered = datasTwo.filter(firstData => {
return datasThree.filter(secondData => firstData.id === secondData.id)
.length
})
} else if (datasThree.length > 0) {
filtered = datasThree
} else if (datasTwo.length > 0) {
filtered = datasTwo
} else if (datasOne.length) {
filtered = datasOne
}
console.log(filtered)
1) You can first filter the array which is not empty in arrs.
const arrs = [datasOne, datasTwo, datasThree].filter((a) => a.length);
2) Flatten the arrs array using flat().
arrs.flat()
3) Loop over the flatten array and count the occurrence of all objects using Map
const map = new Map();
for (let o of arrs.flat()) {
map.has(o.id)
? (map.get(o.id).count += 1)
: map.set(o.id, { ...o, count: 1 });
}
4) Loop over the map and collect the result only if it is equal to arrs.length
if (count === arrs.length) result.push(rest);
let datasOne = [
{ id: 1, name: "Gustavo" },
{ id: 2, name: "Ana" },
{ id: 3, name: "Luiz" },
{ id: 8, name: "Alice" },
];
let datasTwo = [
{ id: 1, name: "Gustavo" },
{ id: 3, name: "Luiz" },
{ id: 8, name: "Alice" },
];
let datasThree = [
{ id: 1, name: "Gustavo" },
{ id: 3, name: "Luiz" },
{ id: 2, name: "Ana" },
{ id: 5, name: "Kelly" },
{ id: 4, name: "David" },
];
const arrs = [datasOne, datasTwo, datasThree].filter((a) => a.length);
const map = new Map();
for (let o of arrs.flat()) {
map.has(o.id)
? (map.get(o.id).count += 1)
: map.set(o.id, { ...o, count: 1 });
}
const result = [];
for (let [, obj] of map) {
const { count, ...rest } = obj;
if (count === arrs.length) result.push(rest);
}
console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
Not 100% sure it cover all edge cases, but this might get you on the right track:
function filterArrays(...args) {
const arraysWithData = args.filter((array) => array.length > 0);
const [firstArray, ...otherArrays] = arraysWithData;
return firstArray.filter((item) => {
for (const array of otherArrays) {
if (!array.some((itemTwo) => itemTwo.id === item.id)) {
return false;
}
}
return true;
});
}
Usage:
const filtered = filterArrays(datasOne, datasTwo, datasThree);
console.log(filtered)
I believe the code is fairly readable, but if something is not clear I'm glad to clarify.
function merge(arr){
arr = arr.filter(item=>item.length>0)
const map = {};
arr.forEach(item=>{
item.forEach(obj=>{
if(!map[obj.id]){
map[obj.id]=[0,obj];
}
map[obj.id][0]++;
})
})
const len = arr.length;
const ret = [];
Object.keys(map).forEach(item=>{
if(map[item][0]===len){
ret.push(map[item][1])
}
})
return ret;
}
merge([datasOne,datasTwo,datasThree])

Multiple Dynamic Filters in JavaScript Array

I can't seem to think about how I can overcome this issue where there might be any amount of filters as objects which will help me to filter out the data array.
data = [
{
id: 1,
first_name: 'Colver',
}, {
id: 2,
first_name: 'Brodie',
}, {
id: 3,
first_name: 'Philippa',
}, {
id: 4,
first_name: 'Taite',
}, {
id: 5,
first_name: 'Pierson'
}
];
filters = [
{
field: 'id',
operator: 'between',
value: '2-5'
},
{
field: 'first_name',
operator: 'eq',
value: 'Philippa'
}
];
ngOnInit(): void {
const filteredItems = [];
this.data.forEach(item => {
this.filters.forEach((filter, filterIndex) => {
const itemValue = item[filter.field];
switch (filter.operator) {
case 'eq':
if (itemValue === filter.value) {
filteredItems.push(item);
}
break;
case 'between':
const [firstValue, secondValue] = filter.value.split('-');
if (itemValue > firstValue && itemValue < secondValue) {
filteredItems.push(item);
}
break;
}
});
});
console.log(filteredItems);
}
I basically want the filteredItems to output like below since the id is between 2 and 5 and the first_name is Philippa. But since I'm iterating the filters 2 times both the times items gets pushed to filteredItems.
[{
id: 3,
first_name: 'Philippa',
}]
You could take Array#every and an object for getting the right operator function.
const
data = [{ id: 1, first_name: 'Colver' }, { id: 2, first_name: 'Brodie' }, { id: 3, first_name: 'Philippa' }, { id: 4, first_name: 'Taite' }, { id: 5, first_name: 'Pierson' }],
filters = [{ field: 'id', operator: 'between', value: '2-5' }, { field: 'first_name', operator: 'eq', value: 'Philippa' }],
operators = {
between: (field, range) => {
const [min, max] = range.split('-').map(Number);
return min <= field && field <= max;
},
eq: (field, value) => field === value
},
result = data.filter(o =>
filters.every(({ field, operator, value }) =>
operators[operator](o[field], value)
)
);
console.log(result);
You can perform a reduce operation over the filters array and use Array#filter to remove objects on each iteration.
const data = [
{
id: 1,
first_name: 'Colver',
}, {
id: 2,
first_name: 'Brodie',
}, {
id: 3,
first_name: 'Philippa',
}, {
id: 4,
first_name: 'Taite',
}, {
id: 5,
first_name: 'Pierson'
}
],
filters = [
{
field: 'id',
operator: 'between',
value: '2-5'
},
{
field: 'first_name',
operator: 'eq',
value: 'Philippa'
}
];
const res = filters.reduce((acc,{field,operator,value})=>
acc.filter(o => operator === 'eq' && o[field] === value ||
operator === 'between' && o[field] >= value.split('-')[0]
&& o[field] <= value.split('-')[1]), data);
console.log(res);
Use Array.prototype.every to make sure every filter passes, and if so, push it to the array:
ngOnInit(): void {
const filteredItems = this.data.forEach(item =>
this.filters.every((filter, filterIndex) => {
const itemValue = item[filter.field];
switch (filter.operator) {
case 'eq':
if (itemValue === filter.value) {
return true;
}
break;
case 'between':
const [firstValue, secondValue] = filter.value.split('-');
if (itemValue > firstValue && itemValue < secondValue) {
return true;
}
break;
}
return false;
})
);
console.log(filteredItems);
}
Instead of using
const filteredItems = [];
this.data.forEach(item => {
// [...]
filteresItems.push(item)
// [...]
});
use Array's filter:
const filteredItems = this.data.filter(item => {
// [...]
let match = true; // or false
return match;
});
Taking your whole example, you could use:
function passes(item, filter) {
const itemValue = item[filter.field];
switch (filter.operator) {
case 'eq':
if (itemValue === filter.value) {
return true;
}
case 'between':
const [firstValue, secondValue] = filter.value.split('-');
if (itemValue > firstValue && itemValue < secondValue) {
return true;
}
}
return false;
}
const filteredItems = this.data.filter(
item => this.filters
.map(filter => passes(item, filter))
.every());

Getting occurrences of different values on nested object

I've an array of objects like this:
arrObj = [{
id: 1
data: {
info: {
name: 'jhon'
}
}
},{
id: 1
data: {
info: {
name: 'jane'
}
}
},{
id: 1
data: {
info: {
name: 'jhon'
}
}
}]
And I needs get a summary of occurrences for different values, like this:
{ jane: 1, jhon: 2 }
The big problem is that I need pass the nested prop dynamically:
getSummary('data.info.name',obj) //--> { jane: 1, jhon: 2 }
Any ideas?
You can use the below code, this is just hint. you need to do error handling if some input is not having correct nested keys.
let arrObj = [{
id: 1,
data: {
info: {
name: 'jhon'
}
}
},{
id: 1,
data: {
info: {
name: 'jane'
}
}
},{
id: 1,
data: {
info: {
name: 'jhon'
}
}
}]
const getSummary = (dynamicKeys,obj) => {
const list = dynamicKeys.split('.');
const op = {};
for (let i = 0; i < obj.length; i++) {
let n = 1, key = obj[i][list[0]];
while (list.length > n) {
key = key[list[n]];
n++;
}
op[key] = op[key] ? op[key] + 1 : 1;
}
return op;
}
const test = getSummary('data.info.name', arrObj);
console.log(test)
A possible solution could be as below. Here at first given prop is found out from each element of arrayObj. If the finding isn't successful, the element is skipped and move to next. When the finding is successful, append the finding value to summary if it does not exist in summary or increment the existing value. You can change the code as your requirements.
const arrObj = [{
id: 1,
data: {
info: {
name: 'jhon'
}
}
}, {
id: 1,
data: {
info: {
name: 'jane'
}
}
}, {
id: 1,
data: {
info: {
name: 'jhon'
}
}
}];
const getSummary = (prop, arr) => {
const keys = prop.split('.');
const findPropValue = (elem) =>
keys.reduce((val, key, index) => {
if (index === 0) return elem[key];
return (val && val[key]) || val
}, null);
return arr.reduce((sum, curr) => {
const key = findPropValue(curr);
if (!key) return sum;
sum[key] = (sum[key] && sum[key] + 1) || 1;
return sum;
}, {});
};
console.log(getSummary('data.info.name', arrObj));
Go over elements using forEach. For each object, access the value and build a res object with keys as value (eg jane) and object values are aggregated.
[Access the value, by split the path, access object nested using reduce)
const getSummary = (path, items) => {
const paths = path.split(".");
const res = {};
items.forEach((item) => {
const value = paths.reduce((acc, cur) => acc[cur], item);
res[value] = (res[value] ?? 0) + 1;
});
return res;
};
arrObj = [
{
id: 1,
data: {
info: {
name: "jhon",
},
},
},
{
id: 1,
data: {
info: {
name: "jane",
},
},
},
{
id: 1,
data: {
info: {
name: "jhon",
},
},
},
];
const output = getSummary("data.info.name", arrObj);
console.log(output);

Remove object in nested structure by key

I have an array with objects, that can have children, the children have the same structure as the parent, it's just object nesting basically.
I'm wondering how I can delete one of the objects by key. For example I want to delete the object with id: 1 (which is nested in a children array of another object)
const data = [
{
id: 2,
children: [
{
id: 1,
children: []
}
]
},
{
id: 3,
children: [],
}
]
Moving children up
Would it be possible? If an object with children is deleted, that the children move up to the root?
I've tried
I tried reworking the following function, which returns all id's from my data structure, so it fetches the id, and if there's children, fetches the id's inside those children. But how do I go about deleting an object with that id?
export function flattenFindAttribute (data, attribute) {
return data.map(item => [item[attribute], ...flattenFindAttribute(item.children)]).flat()
}
You just need to use a recursive function call and use Array#splice() method to remove the searched object.
This is how should be your code:
function removeId(data, id) {
data.forEach((o, i) => {
if (o.id && o.id === id) {
data.splice(i, 1);
return true;
} else if (o.children) {
removeId(o.children, id);
}
});
}
Demo:
const data = [{
id: 2,
children: [{
id: 1,
children: []
}]
},
{
id: 3,
children: [],
}
];
function removeId(data, id) {
data.forEach((o, i) => {
if (o.id && o.id === id) {
data.splice(i, 1);
return true;
} else if (o.children) {
removeId(o.children, id);
}
});
}
removeId(data, 1);
console.log(data);
Edit:
If you want to push all the deleted item children into its parent children array, you just need to pass a third param to your function to keep trace of the parent object:
function removeId(data, id, parent) {
data.forEach((o, i) => {
if (o.id && o.id === id) {
if (parent) {
o.children.forEach(c => parent.children.push(c));
}
data.splice(i, 1);
return true;
} else if (o.children) {
removeId(o.children, id, o);
}
});
}
Demo:
var data = [{
id: 2,
children: [{
id: 1,
children: [1, 2]
}]
},
{
id: 3,
children: [],
}
];
function removeId(data, id, parent) {
data.forEach((o, i) => {
if (o.id && o.id === id) {
if (parent) {
o.children.forEach(c=> parent.children.push(c));
}
data.splice(i, 1);
return true;
} else if (o.children) {
removeId(o.children, id, o);
}
});
}
removeId(data, 1);
console.log(data);
You can do this way:
const data = [
{
id: 2,
children: [
{
id: 1,
children: []
}
]
},
{
id: 3,
children: [],
}
]
let deletedObj = {}
function deleteAtId(arr, deleteId) {
const rs = []
arr.forEach(({id, children}) => {
if(id !== deleteId) {
if(!children.length) {
rs.push({id, children})
} else {
const tmp = deleteAtId(children, deleteId)
rs.push({id, children: tmp})
}
} else deletedObj = {id, children}
})
return rs
}
const rs = [...deleteAtId(data, 1), {...deletedObj}]
console.log(rs)
Something like this? It iterates recursively and splices when it finds an object with your id
function removeObject(data, id){
data.forEach(point =>{
if(point.children.length > 0){
removeObject(point.children, id);
}
const index = data.findIndex(x => x.id == id);
if(index > -1){
data.splice(index ,1);
}
});
return data;
}
const data = [
{
id: 2,
children: [
{
id: 1,
children: []
}
]
},
{
id: 3,
children: [],
}
]
removeObject(data, 1);
console.log(data)
Explore the object recursively and delete according to a specified predicate:
const data =[...Array(4)].map(()=> [
{
id: 2,
children: [
{
id: 1,
children: []
}
]
},
{
id: 3,
children: [],
}
]);
function deleteObj(parent, predicate) {
if (predicate(parent)) {
return true;
}
if (typeof parent === 'object') {
if (Array.isArray(parent)) {
for (let i = 0; i < parent.length; i++) {
if (deleteObj(parent[i], predicate)) {
parent.splice(i, 1);
i--;
}
}
} else {
Object.keys(parent).forEach(key => deleteObj(parent[key], predicate) && delete parent[key]);
}
}
return false;
}
console.log('from array:', data[0]);
const test1 = data[1];
const test2 = data[2];
const test3 = data[3];
console.log('delete node with id === 1');
deleteObj(test1, node => node.id === 1);
console.log(test1);
console.log('delete node with id === 3');
deleteObj(test2, node => node.id === 3);
console.log(test2);
console.log('delete node with non empty children');
deleteObj(test3, node => node.children && node.children.length > 0);
console.log(test3);
You can achieve that using Array.reduce with a recursion on the children.
Example:
const data = [
{ id: 1, children: [{ id: 2, children: [] }] },
{
id: 2,
children: [
{
id: 1,
children: [],
},
],
},
{
id: 3,
children: [],
},
{
id: 4,
children: [
{
id: 2,
children: [
{
id: 2,
children: [
{
id: 1,
children: [],
},
{
id: 2,
children: [],
},
],
},
],
},
],
},
];
function removeBy(data, predicate) {
return data.reduce((result, item) => {
if (!predicate(item.id)) {
const newItem = { ...item };
if (item.children.length > 0) {
newItem.children = removeBy(item.children, predicate);
}
result.push(newItem);
}
return result;
}, []);
}
const predicate = value => value === 1;
const result = removeBy(data, predicate);
console.log(result);
This recursive function work for your needings:
function rotateChildren(array, key) {
if (!array || !array.length) {
return array;
}
return array.filter(child => {
if (child) {
child.children = rotateChildren(child.children, key);
return child.id !== key;
}
return !!child;
});
}
const data = [
{
id: 2,
children: [
{
id: 1,
children: []
}
]
},
{
id: 3,
children: [],
}
];
console.log(rotateChildren(data, 1));
console.log(rotateChildren(data, 2));
console.log(rotateChildren(data, 3));

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]}];
}, []);

Categories