简体   繁体   English

将两个数组中的2个对象组合成一个循环?

[英]Combine 2 objects from 2 arrays in a loop?

I'm creating a deck of cards (an array of card objects) by combining an array of suit objects, and an array of card objects, using javascript. 我正在通过使用javascript组合西装对象数组和纸牌对象数组来创建一副纸牌(纸牌对象数组)。

I'm using a forEach loop to loop through the suits, and nested is a map loop for the cards. 我正在使用forEach循环遍历西装,并且嵌套的是卡片的映射循环。

The console.log is returning the correct object to push to the new array, however the .push() is only appending a combined object using the last suit and the last card. console.log返回正确的对象以推送到新数组,但是.push()仅使用最后一个西装和最后一个卡来附加组合的对象。

Where am I going wrong with this? 我在哪里错呢?

I have tried multiple different loops and methods to append to a new array without luck. 我尝试了多种不同的循环和方法来添加到新数组中而没有运气。 Console.log() returns the correct value, but I am unable to push the correct combined objects to a new array. Console.log()返回正确的值,但是我无法将正确的组合对象推送到新数组。

  // Deck of Cards var suits = [ { suit: "clubs", color: "black" }, { suit: "spades", color: "black" }, { suit: "hearts", color: "red" }, { suit: "diamonds", color: "red" } ]; var family = [ { name: "2", value: 2 }, { name: "3", value: 3 }, { name: "4", value: 4 }, { name: "5", value: 5 }, { name: "6", value: 6 }, { name: "7", value: 7 }, { name: "8", value: 8 }, { name: "9", value: 9 }, { name: "10", value: 10 }, { name: "J", value: 10 }, { name: "Q", value: 10 }, { name: "K", value: 10 }, { name: "A", value: 1 }, ]; var deck = new Array(); suits.forEach(function (x) { var arr = family.map((y) => { var obj = Object.assign(x, y); console.log(obj); deck.push(obj); return obj; }); console.log(arr); }); console.log(deck); 

try Object.assign({}, x, y) instead of Object.assign(x, y) . 尝试使用Object.assign({}, x, y)而不是Object.assign(x, y) Currently you're manipulating the object that is x, by adding all properties of y to it. 当前,您正在通过将y的所有属性添加到x中来操纵它。

 // Deck of Cards var suits = [ {suit: "clubs",color: "black"}, {suit: "spades",color: "black"}, {suit: "hearts",color: "red"}, {suit: "diamonds",color: "red"} ]; var family = [ {name: "2",value: 2}, {name: "3",value: 3}, {name: "4",value: 4}, {name: "5",value: 5}, {name: "6",value: 6}, {name: "7",value: 7}, {name: "8",value: 8}, {name: "9",value: 9}, {name: "10",value: 10}, {name: "J",value: 10}, {name: "Q",value: 10}, {name: "K",value: 10}, {name: "A",value: 1}, ]; var deck = new Array(); suits.forEach(function(x){ var arr = family.map( (y) => { var obj = Object.assign({}, x, y); deck.push(obj); return obj; }); }); console.log(deck); 

Object.assign(x,y) will put the values of y onto x . Object.assign(x,y)将投入的值yx You want to leave x alone, so store your properties in a new Object using Object.assign({}, x,y) . 您希望不考虑x ,因此请使用Object.assign({}, x,y)将属性存储在新的Object中。 Consider the demo below: 考虑下面的演示:

 var suits = [ {suit: "clubs",color: "black"}, {suit: "spades",color: "black"}, {suit: "hearts",color: "red"}, {suit: "diamonds",color: "red"} ]; var family = [ {name: "2",value: 2}, {name: "3",value: 3}, {name: "4",value: 4}, {name: "5",value: 5}, {name: "6",value: 6}, {name: "7",value: 7}, {name: "8",value: 8}, {name: "9",value: 9}, {name: "10",value: 10}, {name: "J",value: 10}, {name: "Q",value: 10}, {name: "K",value: 10}, {name: "A",value: 1}, ]; const tmp = suits.reduce((acc, s) => { return acc.concat(family.map(f => { return Object.assign({}, f, s); })); }, []); const pre = document.createElement("pre"); pre.innerHTML = JSON.stringify(tmp, null, 4); document.body.appendChild(pre); 

If you can use flatmap , then it can be made significantly simpler: 如果可以使用flatmap ,那么它可以变得非常简单:

 const suits = [{ suit: "clubs", color: "black" }, { suit: "spades", color: "black" }, { suit: "hearts", color: "red" }, { suit: "diamonds", color: "red" }] const family = [{ name: "2", value: 2 }, { name: "3", value: 3 }, { name: "4", value: 4 }, { name: "5", value: 5 }, { name: "6", value: 6 }, { name: "7", value: 7 }, { name: "8", value: 8 }, { name: "9", value: 9 }, { name: "10", value: 10 }, { name: "J", value: 10 }, { name: "Q", value: 10 }, { name: "K", value: 10 }, { name: "A", value: 1 }] const deck = suits.flatMap(s => family.map(f => ({...s, ...f}))) console.log(deck) 

A side note, there seems to be a strong convention (presumably from Bridge) of ordering the suits clubs/diamonds/hearts/spades. 旁注,订购西服棍棒/钻石/心形/黑桃似乎有很强的约定(大概是布里奇的)。 In English that's easy to remember since they're alphabetic.) 用英语很容易记住,因为它们是字母。

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

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