Home > Software engineering >  Repeat every element in array based on object properties
Repeat every element in array based on object properties

Time:08-24

I have an array that I'm retrieving from an API. The array looks like this:

[{
    "name": "Rachel",
    "count": 4,
    "fon": "46-104104",
    "id": 2
},
{
    "name": "Lindsay",
    "count": 2,
    "fon": "43-053201",
    "id": 3
},
{
    "name": "Michael",
    "count": 5,
    "fon": "46-231223",
    "id": 4
}]

Then I loop through the array to create an array containing only the names.

function buildName(data) {
  for (var i = 0; i < data.length; i  ) {
    nameList.push(data[i].name)
  }
}

This also works so far, but I would like to create an array in which each name occurs as often as the object count says.

For example, the name Michael should appear five times in the array and Lindsay twice.

[
  "Rachel",
  "Rachel",
  "Rachel",
  "Rachel",
  "Lindsay",
  "Lindsay",
  "Michael",
  "Michael",
  "Michael",
  "Michael"
  "Michael"
]

CodePudding user response:

For each object create a new array using count, and then fill it with the name.

If you use flatMap to iterate over the array of objects. It will return a new array of nested objects but then flatten them into a non-nested structure.

const data=[{name:"Rachel",count:4,fon:"46-104104",id:2},{name:"Lindsay",count:2,fon:"43-053201",id:3},{name:"Michael",count:5,fon:"46-231223",id:4}];

const out = data.flatMap(obj => {
  return new Array(obj.count).fill(obj.name)
});

console.log(out);

CodePudding user response:

I've upgraded your functions but you can use the map method

function buildName(data){
    for (let i = 0; i < data.length; i  ){
      let numToLoop = data[i].count
      let name = data[i].name
        for (let z = 0; z <  numToLoop; z  ){
          nameList.push(name)
          }
      }
 }

CodePudding user response:

Use an inner while loop inside the for loop:

const data = [{
    "name": "Rachel",
    "count": 4,
    "fon": "46-104104",
    "id": 2
},
{
    "name": "Lindsay",
    "count": 2,
    "fon": "43-053201",
    "id": 3
},
{
    "name": "Michael",
    "count": 5,
    "fon": "46-231223",
    "id": 4
}]


function buildName(data){
  const result = [];
  for (let i = 0; i < data.length; i  = 1) {
    let item = data[i];
    let count = item.count;
    while (count > 0) {
      result.push(item.name);
      count -= 1;
    }
  }
  return result;
}


console.log(buildName(data));

CodePudding user response:

Just add an inner loop with as many iterations as the "count" property in the object:

function buildName(data) {
    const nameList = [];

    for (var i = 0; i < data.length; i  ) {
        for (let j = 0; j < data[i].count; j  ) {
            nameList.push(data[i].name);
        }
    }

    return nameList;
}

CodePudding user response:

For fun

import { pipe } from 'fp-ts/lib/function';
import { chain, replicate } from 'fp-ts/lib/Array';

const arr = ...
const result = pipe(
  arr,
  chain(i => replicate(i.count, i.name))
);

CodePudding user response:

You can use .flapMap() for that:

const arr = [{ "name": "Rachel", "count": 4, "fon": "46-104104", "id": 2 }, { "name": "Lindsay", "count": 2, "fon": "43-053201", "id": 3 }, { "name": "Michael", "count": 5, "fon": "46-231223", "id": 4 }];

const result = arr.flatMap(({count, name}) => Array(count).fill(name));
console.log(result);

Effectively you turn every element into an array of the the name property repeated count times which is then flattened into a single array.

CodePudding user response:

It can be done via creating an array with repeated names in this way:

Array(count).fill(name)

Then you have to spread it into resulting array. You can try this one-liner

const getNames = (data) =>
  data.reduce(
    (names, { name, count }) => [...names, ...Array(count).fill(name)],
    []
  )

Note that a pure function is presented here, which is generally the preferred way of writing code. However, updating your example code might look like this

const getNames = (data) =>
  data.reduce(
    (names, { name, count }) => [...names, ...Array(count).fill(name)],
    []
  )

function buildName(data) {
  nameList = getNames(data)
}
      
  • Related