Home > Software engineering >  Duplicate an item in an array basing on the sub-items attached to one of that item's keys in Ja
Duplicate an item in an array basing on the sub-items attached to one of that item's keys in Ja

Time:12-01

I have an array of objects that looks like this

{name:"name1", phoneNumbers:[{"home":"0700 999999"}, {"mobile":"0700 999998"}]}, {name:"name2", phoneNumbers:[{"home":"0700 999991"}, {"mobile":"0700 999995"}, fax:"48289299200"]},
{name:"name3", phoneNumbers:[{"home":"0700 999992"}, {"mobile":"0700 999988"}]}
]

What I want to achieve is something like this

{name:"name1", phoneNumbers:[{"home":"0700 999999"}]},
{name:"name1", phoneNumbers:[{"mobile":"0700 999998"}]},
{name:"name2", phoneNumbers:[{"home":"0700 999991"}]},
{name:"name2", phoneNumbers:[{"mobile":"0700 999995"}]},
{name:"name2", phoneNumbers:[{fax:"48289299200"}]},
...
]

This way, every name will be listed again with each of the contact means attached to it.

I approached this way and I found it challenging to get the solution to work since the outer loop doesn't wait for the inner loop to run exhaustively before it can continue to the next index:

const doSomeRecursion=(arr, i, result)=>{
 for(let j=0; j<arr[i]["phoneNumbers"].length; j  ){                
    arr[i]["phoneNumbers"]=[arr[i]["phoneNumbers"][j]]
      result.push(arr[i])
    }
 }


const getAllContacts=(contacts)=>{

  const reconstructedContacts=[]
  for(let i=0; i<contacts.length; i  ){
    doSomeRecursion(contacts, i, reconstructedContacts)
  }

  return reconstructedContacts
}

First off, this method should give me the desired result but I'm not sure why not. I also think my solution may not be the best way to go about it and I would love to know how you guys would approach this?

Edit: Thanks, @T.J. Crowder, I have corrected the question and wrapped it in a function.

CodePudding user response:

The initial code block in your question has multiple syntax errors, so it's hard to be sure what your starting structure is, but making reasonable guesses at it, there's no need for recursion here at all, just nested loops:

const result = [];
for (const {name, phoneNumbers} of original) {
    for (const number of phoneNumbers) {
        result.push({name, phoneNumbers: number});
    }
}

Live Example:

Show code snippet

const original = [
    {
        name: "name1",
        phoneNumbers: [
            { "home": "0700 999999" },
            { "mobile": "0700 999998" }
        ]
    },
    {
        name: "name2",
        phoneNumbers: [
            { "home": "0700 999991" },
            { "mobile": "0700 999995" },
            {fax: "48289299200"}
        ]
    },
    {
        name: "name3",
        phoneNumbers: [
            { "home": "0700 999992" },
            { "mobile": "0700 999988" }
        ]
    }
];

const result = [];
for (const {name, phoneNumbers} of original) {
    for (const number of phoneNumbers) {
        result.push({name, phoneNumbers: number});
    }
}

console.log(JSON.stringify(result, null, 4));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You could also accomplish this using a combination of flatMap() and map()

const data = [
  {name:"name1", phoneNumbers:[{"home":"0700 999999"}, {"mobile":"0700 999998"}]}, 
  {name:"name2", phoneNumbers:[{"home":"0700 999991"}, {"mobile":"0700 999995"}, 
  {"fax":"48289299200"}]},
  {name:"name3", phoneNumbers:[{"home":"0700 999992"}, {"mobile":"0700 999988"}]}
];

const result = data.flatMap((n) => {
  return n.phoneNumbers.map((pn) => {
    return { name: n.name, phoneNumbers: [pn] }
  });
});

console.log(result);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related