combining duplicate values in JavaScript array

Alan Nama Yosuf

I have A json Object that contain some keys/values one of the values is an array I want combining the value of array from all recorded that have same key this is my Json Object

data= 
[
 {"key":11,"numbers":[555,556,557]},
 {"key":22,"numbers":[444,772,995,667]},
 {"key":11,"numbers":[223,447]},
 {"key":45,"numbers":[999,558,367]}
]

I want to get the result be like this

data= 
[
   {"key":11,"numbers":[555,556,557,223,447]},
   {"key":22,"numbers":[444,772,995,667]},
   {"key":45,"numbers":[999,558,367]}
]

I tried but I don't know how to cache the value of current matched row and try to find another matched value this is my code

data= 
[
 {"key":11,"numbers":[555,556,557]},
 {"key":22,"numbers":[444,772,995,667]},
 {"key":11,"numbers":[223,447]},
 {"key":45,"numbers":[999,558,367]}
];
function findDuplicates(data) {

 let result = [];

data.forEach(function(element, index) {

if (data.indexOf(element, index + 1) > -1) { 
  if (result.indexOf(element) === -1) {

    result.push(element.numbers.concat(data.indexOf(element, index + 1));
  }
}
});

return result;
}

console.log( findDuplicates(data) ); 
Eddie

You can use reduce to group the array into an object. Use concat to merge arrays. Use Object.values to convert the object to array.

let data = [
  {"key":11,"numbers":[555,556,557]},
  {"key":22,"numbers":[444,772,995,667]},
  {"key":11,"numbers":[223,447]},
  {"key":45,"numbers":[999,558,367]},
  {"key":45,"numbers":100}
];

let result = Object.values(data.reduce((c, {key,numbers}) => {
  c[key] = c[key] || {key,numbers: []};
  c[key].numbers = c[key].numbers.concat(Array.isArray(numbers) ? numbers : [numbers]); 
  return c;
}, {}));

console.log(result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Combining objects with duplicate values inside an array

combining rows for duplicate values

javascript duplicate array and change values

Pandas: combining duplicate index values

Array Combining Values

combining array values in looping

Duplicate, Distinct and Unique values in Array JavaScript

Find duplicate values in javascript associative array (hash)

Remove duplicate values from an array of objects in javascript

How to find the most duplicate "values" in javascript array?

Combine duplicate values ​by date in JavaScript array

Show duplicate index for duplicate values in an array of objects in javascript

How to count values in a javascript object (it is not an array)? [not duplicate] [not duplicate] [NOT

Combining object values based on another that are duplicate

Combining objects in an array with similar values

Determining duplicate values in an array

Calculating the Duplicate Values in an Array

Merge duplicate values in array

flattened array with duplicate values

Duplicate values in array

Find duplicate object values in an array and merge them - JAVASCRIPT

Count duplicate values in an array and return count with an additional value using Javascript

How to remove duplicate data values from an array of objects in javascript?

Counting duplicate JSON array values using hashmap with javascript

How get the probability of shuffling an array without getting duplicate Values in javascript

In Javascript, how do I check if an array has duplicate values?

In JavaScript, how do I check if an array has duplicate multiple values?

Javascript: How to remove only one value from duplicate array values

How to find sum of duplicate values in an array using javascript