简体   繁体   中英

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

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

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 }]
    }
});

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);

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)

As filipe said in his comment, Your current code is just assigning the values into an object which results in maintaining the single value against keys. To group the same profession values into a respective profession key, You have to do something like this :

 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].push({ Name, age }): result[profession] = [{ Name, age }]; }); console.log(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM