将所有玩家的列表重新排列到一个数组中

时间:2018-04-09 13:04:06

标签: javascript arrays object

我有这个json对象

var server = [{
  name: 'xVg1',
  players: ['foo', 'bar','player1'],
  status: "on",
  origin:"",
}, {
  name: 'xVg2',
  players: ['foo1', 'bar1','player2'],
  status: "off",
  origin:"",
}, {
  name: 'xVg3',
  players: ['foo2', 'bar2','player3'],
  status: "on",
  origin:""
}];

我正在寻找一种有效的方法从这个对象中将玩家的所有名称检索到一个数组中,我需要实现这个结果

player=['foo', 'bar','player1','foo1', 'bar1','player2','foo2', 'bar2','player3']

我用于每个循环,但它没有实现我想要的结果是一个数组数组,我希望它成为一个单独的数组,就像上面的结果

var arr=[]
server.forEach(val =>{
   arr.push(val.players)
})

7 个答案:

答案 0 :(得分:1)

使用Array.concat

更新
var server = [{
  name: 'xVg1',
  players: ['foo', 'bar','player1'],
  status: "on",
  origin:"",
}, {
  name: 'xVg2',
  players: ['foo1', 'bar1','player2'],
  status: "off",
  origin:"",
}, {
  name: 'xVg3',
  players: ['foo2', 'bar2','player3'],
  status: "on",
  origin:""
}];

var arr=[]
server.forEach(val =>{
   arr = arr.concat(val.players)
})

console.log(arr);

{{1}}



{{1}}




答案 1 :(得分:1)

使用reduce和spread运算符,您可以在一行代码中实现结果



 var server = [{
      name: 'xVg1',
      players: ['foo', 'bar','player1'],
      status: "on",
      origin:"",
    }, {
      name: 'xVg2',
      players: ['foo1', 'bar1','player2'],
      status: "off",
      origin:"",
    }, {
      name: 'xVg3',
      players: ['foo2', 'bar2','player3'],
      status: "on",
      origin:""
    }];
    
    var players = server.reduce((x,y)=>[...x,...y.players],[]);
    console.log(players)




答案 2 :(得分:1)

您可以将reduce().concat()

一起使用
let result = server.reduce((a, c) => a.concat(c.players), []);

或使用Spread语法:

let result = server.reduce((a, c) => (a.push(...c.players), a), []);

<强>演示:

let server = [{name: 'xVg1', players: ['foo', 'bar','player1'], status: "on", origin:""}, {name: 'xVg2', players: ['foo1', 'bar1','player2'], status: "off", origin:""}, {name: 'xVg3', players: ['foo2', 'bar2','player3'], status: "on", origin:""}];

let result = server.reduce((a, c) => a.concat(c.players), []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

<强>文档:

答案 3 :(得分:1)

使用点差

var server = [{
  name: 'xVg1',
  players: ['foo', 'bar','player1'],
  status: "on",
  origin:"",
}, {
  name: 'xVg2',
  players: ['foo1', 'bar1','player2'],
  status: "off",
  origin:"",
}, {
  name: 'xVg3',
  players: ['foo2', 'bar2','player3'],
  status: "on",
  origin:""
}];

var res = server.reduce((acc, c) => {
  acc.push(...c.players);
  return acc;
}, []);

console.log(res);

使用concat

var server = [{
  name: 'xVg1',
  players: ['foo', 'bar','player1'],
  status: "on",
  origin:"",
}, {
  name: 'xVg2',
  players: ['foo1', 'bar1','player2'],
  status: "off",
  origin:"",
}, {
  name: 'xVg3',
  players: ['foo2', 'bar2','player3'],
  status: "on",
  origin:""
}];
var res = [];
server.forEach((obj) => {
  res = res.concat(obj.players);
});

console.log(res);

答案 4 :(得分:1)

这可以是处理它的另一种方式。只是为了好玩:)

&#13;
&#13;
$data->networks[0]->ipAddress
&#13;
&#13;
&#13;

答案 5 :(得分:1)

当您使用forEach()时,forEach()内部可以使用Spread_syntax

<强>样本

const server = [{
  name: 'xVg1',
  players: ['foo', 'bar','player1'],
  status: "on",
  origin:"",
}, {
  name: 'xVg2',
  players: ['foo1', 'bar1','player2'],
  status: "off",
  origin:"",
}, {
  name: 'xVg3',
  players: ['foo2', 'bar2','player3'],
  status: "on",
  origin:""
}];

let result = [];
server.forEach(obj=>{
	result.push(...obj.players);
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

您可以使用reduce()Spread syntax来获得所需的结果。

<强>样本

const server = [{
  name: 'xVg1',
  players: ['foo', 'bar', 'player1'],
  status: "on",
  origin: "",
}, {
  name: 'xVg2',
  players: ['foo1', 'bar1', 'player2'],
  status: "off",
  origin: "",
}, {
  name: 'xVg3',
  players: ['foo2', 'bar2', 'player3'],
  status: "on",
  origin: ""
}]


let result = server.reduce((arr, object) => [...arr, ...object.players], []);

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 6 :(得分:1)

使用Underscore.js库提供一大堆有用的函数式编程助手,而不扩展任何内置对象。

var a = [{
      name: 'xVg1',
      players: ['foo', 'bar','player1'],
      status: "on",
      origin:"",
    }, {
      name: 'xVg2',
      players: ['foo1', 'bar1','player2'],
      status: "off",
      origin:"",
    }, {
      name: 'xVg3',
      players: ['foo2', 'bar2','player3'],
      status: "on",
      origin:""
    }];

    var result = _.flatten(_.pluck(a,"players"));
    
    console.log(result);