Home > other >  How to move similar profession people into newly created object (result) in javascript
How to move similar profession people into newly created object (result) in javascript

Time:08-27

I have one quick question, I want to move all similar professional people into a newly created object (result), I have written the code but all values are not moved.

const data = [
    {
        Name: 'Smith',
        age: 25,
        profession: 'Banker'
    },
    {
        Name: 'Alex',
        age: 28,
        profession: 'IT'
    },
    {
        Name: 'John',
        age: 31,
        profession: 'Banker'
    },
    {
        Name: 'Harry',
        age: 26,
        profession: 'Nurse'
    },
];
const result = {};

My code is here ...

const data = [ { Name: "Smith", age: 25, profession: "Banker" }, { Name: "Alex", age: 28, profession: "IT" }, { Name: "John", age: 31, profession: "Banker" }, { Name: "Harry", age: 26, profession: "Nurse" } ];

const result = {};
data.forEach(({ Name, age, profession }) => {
  result[profession] = { Name, age };
});
console.log(result);

CodePen: https://codepen.io/Sandy4405/pen/wvmLaJX

CodePudding user response:

Expanding on my comment above, the inside of your forEach should be:

data.forEach(({ Name, age, profession }) => {
    if (Array.isArray(result[profession])) {
        result[profession].push({ Name, age })
    } else {
        result[profession] = [{ Name, age }]
    }
});

CodePudding user response:

You need to create a nested JSON, which means an array of objects for a similar profession. While iterating, create an array for each profession and use .push() to add the objects. The code would look like below

data.forEach(({Name,age,profession}) => {
  result[profession] = result[profession] || [];
  result[profession].push({Name,age});
});

Working Version:

const data = [{
    Name: "Smith",
    age: 25,
    profession: "Banker"
  },
  {
    Name: "Alex",
    age: 28,
    profession: "IT"
  },
  {
    Name: "John",
    age: 31,
    profession: "Banker"
  },
  {
    Name: "Harry",
    age: 26,
    profession: "Nurse"
  }
];
const result = {};
data.forEach(({
  Name,
  age,
  profession
}) => {
  result[profession] = result[profession] || [];
  result[profession].push({
    Name,
    age
  });
});
console.log(result);

CodePudding user response:

After the first person with a given profession is encountered, each subsequent person with the same profession will overwrite the previous one. You should instead create an array and push each of the people with the same profession to that array.

First, we'll check if the current profession has been encountered before. If it hasn't, we'll create an empty array to hold all of the people with this profession.

if (!(profession in result)) {
  result[profession] = []
}

Then, since the array is now guaranteed to exist for this profession, we can push to our new array the current person. The next time this profession is encountered, our first check will be skipped and we'll just push the next person onto the array.

result[profession].push({
  Name,
  age
})

Full example:

const data = [{
  Name: "Smith",
  age: 25,
  profession: "Banker"
}, {
  Name: "Alex",
  age: 28,
  profession: "IT"
}, {
  Name: "John",
  age: 31,
  profession: "Banker"
}, {
  Name: "Harry",
  age: 26,
  profession: "Nurse"
}]

const result = {}

data.forEach(({
  Name,
  age,
  profession
}) => {
  if (!(profession in result))
    result[profession] = []

  result[profession].push({
    Name,
    age
  })
})
console.log(result)

  • Related