简体   繁体   English

javascript中如何将同类职业的人搬入新创建的object(结果)

[英]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.我有一个简单的问题,我想将所有类似的专业人员移入一个新创建的 object(结果),我已经编写了代码,但所有值都没有移动。

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代码笔: https://codepen.io/Sandy4405/pen/wvmLaJX

Expanding on my comment above, the inside of your forEach should be:扩展我上面的评论,你的forEach内部应该是:

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.您需要创建一个nested JSON ,这意味着类似职业的对象数组。 While iterating, create an array for each profession and use .push() to add the objects.迭代时,为每个职业创建一个数组并使用.push()添加对象。 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.正如filipe在他的评论中所说,您当前的代码只是将值分配到 object 中,这会导致针对键维护单个值。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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