在for循环JavaScript中将键的值从数组推送到另一个数组

时间:2018-01-17 14:17:14

标签: javascript arrays vue.js

我正在尝试从第一个数组中存在的键中推送一个值。这个数组包含一个country_users数组,我已遍历这个数组中的每个用户数组,并将这些数组推送到一个名为users的新数组

在此演示中,我尝试将countrycountry_users推送到users数组中的每个用户对象。

我为此目的使用Vue.js,使用Vue可能更容易?有什么建议吗?



var users = [];
var country_users = [
  {
    "country": "United States",
    "users": [
      {
        "name": "John"
      },
      {
        "name": "Jane"
      },
    ],
  },
  
  {
    "country": "Norway",
    "users": [
      {
        "name": "Ola"
      },
      {
        "name": "Jens"
      },
    ],
  },
  
];

// Here I push all users from country_users into a new array called users
for (let cu = 0; cu < country_users.length; cu++) {
  for (let u = 0; u < country_users[cu].users.length; u++) {
  users.push(country_users[cu].users[u]);
  }
  
  // Do I make a new for loop here to push the country into every object inside users array? How?
}

console.log(users);
&#13;
&#13;
&#13;

我想将国家从之前用户所属的数组中推出。

我想要的结果是users

这样的数组

&#13;
&#13;
var users = [
 {
"name": "John",
"country": "United States"
  },
  {
"name": "Jane",
"country": "United States"
  },
  {
"name": "Ola",
"country": "Norway"
  },
  {
"name": "Jens",
"country": "Norway"
  }
];

console.log(users);
&#13;
&#13;
&#13;

&#13;
&#13;
var users = [];
var country_users = [
  {
    "country": "United States",
    "users": [
      {
        "name": "John"
      },
      {
        "name": "Jane"
      },
    ],
  },
  
  {
    "country": "Norway",
    "users": [
      {
        "name": "Ola"
      },
      {
        "name": "Jens"
      },
    ],
  },
  
];

country_users.forEach((country_user) => {
  country_user.users.forEach((user) => {
     user.country = country_user.country;
     users.push(user);
  });
});

console.log(users);
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:2)

您可以迭代给定的数组及其用户数组,并构建一个包含所需属性的新数组。

&#13;
&#13;
var country_users = [{ country: "United States", users: [{ name: "John" }, { name: "Jane" }] }, { country: "Norway", users: [{ name: "Ola" }, { name: "Jens" }] }],
    users = country_users.reduce(function (r, country) {
        country.users.forEach(function (user) {
            r.push({ name: user.name, country: country.country });
        });
        return r;
    }, []);
    
console.log(users);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

ES6

&#13;
&#13;
var country_users = [{ country: "United States", users: [{ name: "John" }, { name: "Jane" }] }, { country: "Norway", users: [{ name: "Ola" }, { name: "Jens" }] }],
    users = country_users.reduce((r, { country, users }) => {
        users.forEach(({ name }) => r.push({ name, country }));
        return r;
    }, []);
    
console.log(users);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用Array.prototype.reduce,并在每次迭代中创建并返回包含用户和国家/地区的新数组:

const country_users = [
  {
    "country": "United States",
    "users": [
      {
        "name": "John"
      },
      {
        "name": "Jane"
      },
    ],
  }, 
  {
    "country": "Norway",
    "users": [
      {
        "name": "Ola"
      },
      {
        "name": "Jens"
      },
    ],
  },  
];

const result = country_users.reduce((prev, curr) => {
  const users = curr.users.map(user => ({
    name: user.name,
    country: curr.country
  }))
  
  return [
    ...prev,
    ...users,
  ]
}, [])

console.log(result)

答案 2 :(得分:0)

只需在推送

之前添加属性国家/地区

var users = [];
var country_users = [
  {
    "country": "United States",
    "users": [
      {
        "name": "John"
      },
      {
        "name": "Jane"
      },
    ],
  },
  
  {
    "country": "Norway",
    "users": [
      {
        "name": "Ola"
      },
      {
        "name": "Jens"
      },
    ],
  },
  
];

// Here I push all users from country_users into a new array called users
for (let cu = 0; cu < country_users.length; cu++) {
  for (let u = 0; u < country_users[cu].users.length; u++) {
     var newUser = country_users[cu].users[u];
     newUser.country = country_users[cu].country;
     users.push(newUser);
  }
  
  // Do I make a new for loop here to push the country into every object inside users array? How?
}

console.log(users);

答案 3 :(得分:0)

建议您使用ES6语法:

import QtQuick 2.9
import QtQuick.Controls 2.3

ApplicationWindow {
    visible: true

    RoundButton {
        id: button
        text: "ah"

        palette.button: "salmon"
    }
}

使用rest运算符&#34; ...&#34;

复制用户对象