Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

id multiple same property array combining

var originalArray = [{
  id: 1,
  elements: [1, 2]
},
{
  id: 1,
  elements: [3, 4]
},
{
  id: 5,
  elements: ['a', 'b']
},
{
  id: 5,
  elements: ['c', 'd']
}, {
  id: 27,
  elements: []
}]

var newArray = [];
for (obj of originalArray) {

    var empty = true;
    for (newobj of newArray) {
       if (obj.id == newobj.id) { empty = false; }
    }
    if (empty) {
        newArray.push({id: obj.id, elements: obj.elements});
    } else {
        for (newobj of newArray) {
           if (newobj.id == obj.id) {
               for (o of obj.elements) {
                 newobj.elements.push(o);
                }
           }
        }
    }
}
console.log(newArray);
 Run code snippetHide resultsExpand snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: babel core cdn 
Javascript :: zalgo text in javascript 
Javascript :: how to convert roman to decimal in javascript 
Javascript :: Run FEnvQueryRequest 
Javascript :: strip whitespace from shopify liquid output 
Javascript :: EFSavechanges 
Javascript :: why null is an object in javascript 
Javascript :: how dynamique pseudo element in react 
Javascript :: javascript type 
Javascript :: how to make navbar links scroll to component react 
Javascript :: javascript include too large or too small numbers 
Javascript :: javascript Tagged Templates 
Javascript :: javascript setTimeout() method returns the interval id 
Javascript :: can i copy package-lock.json to another project 
Javascript :: react destructuring with rename 
Javascript :: card types regex 
Javascript :: convert string to slug javascript 
Javascript :: manter alguns campos objetos javascript 
Javascript :: change xy scale phaser 
Javascript :: phaser rotate container facing point 
Javascript :: phaser sprite animation event 
Javascript :: core.mjs:4057 JIT compilation failed for NgModule class AppModule 
Javascript :: get random hsl color js 
Javascript :: rxact 6 number long in yup validation 
Javascript :: Self Invoking Function ($()) That Can Be Reused 
Javascript :: how to declare a variable js 
Javascript :: function generator js 
Javascript :: string charat 
Javascript :: javascript practice questions 
Javascript :: return the first element in an array javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =