如何迭代对象数组并连接内容

时间:2017-11-14 09:02:40

标签: javascript arrays

我有下面发布的对象(obj1)。如图所示,它包含一个名为" favoritePlayers"。

的对象数组

我要做的是,创建一个函数或添加一个数组原型,以便我可以分别迭代数组中的每个元素并获得以下输出:

"梅西在巴塞罗那演出" " totti在罗马演出"

我试图解决它,如下所示,但我得到的是

[ 'undefinedplays in[object Object]',
'undefinedplays in[object Object]' ]

OBJ1

var obj1 = {
score: [10, 22, 30, 55, 70, 90],
lettersOfMyName: ["a", "m", "r"],
avgScore: null,
favoritePlayers: [
    {
        name: "messi",
        club: "barcelona"
    },

    {
        name: "totti",
        club: "roma"
    }
],

calcAvgScore: function () {
    var sumOfScores = this.score.reduce(function (prev, cur, index, array)   
{

        return prev + cur;

    })

    return (sumOfScores / this.score.length);
},

myFavoritePlayersInfo: function () {
    console.log(this.favoritePlayers.join());
},

}

我的尝试:     //此代码是在obj1

范围之外添加的
Array.prototype.concat1 = ( (array) => {
var tempArray = [];

for (var i = 0; i < array.length; i++) {

tempArray[i] = array.slice(i, i+1).name + "plays in" + array.slice(i,   
i+1);

}

return tempArray;

});

update_1

我将以下行添加到obj1:

getPlaysIn:function() {
    this.favoritePlayers.forEach( (favoritePlayer)=> {
        return favoritePlayer.name + " plays in " + favoritePlayer.club;

我将其称为如下:

console.log(obj1.getPlaysIn());

但我得到的结果是

undefined

5 个答案:

答案 0 :(得分:1)

您可以使用Array#map来迭代播放器,并创建所需的阵列:

Vue.component('Modal', 
{
  template: '#modal-template',props: ['open','close', 'component', 'data'],
  mounted: function () {
      console.log(this.data)
  },
 methods: {
  testing: function(){
   alert('clicked')
   this.$emit('childHandler','hehe')
  }
 }
});

答案 1 :(得分:0)

只需迭代favoritePlayers数组

&#13;
&#13;
var obj1 = {
  score: [10, 22, 30, 55, 70, 90],
  lettersOfMyName: ["a", "m", "r"],
  avgScore: null,
  favoritePlayers: [{
      name: "messi",
      club: "barcelona"
    },

    {
      name: "totti",
      club: "roma"
    }
  ]
}
obj1.favoritePlayers.forEach(function(item) {
  console.log(item.name + ' plays ' + item.club)
})
&#13;
&#13;
&#13;

如果你想使用原型

&#13;
&#13;
var obj1 = {
  score: [10, 22, 30, 55, 70, 90],
  lettersOfMyName: ["a", "m", "r"],
  avgScore: null,
  favoritePlayers: [{
      name: "messi",
      club: "barcelona"
    },

    {
      name: "totti",
      club: "roma"
    }
  ]
}
Array.prototype.customForEach = function() {
  for (let i = 0; i < this.length; i++) {
    console.log(this[i].name + ' plays ' + this[i].club);

  }
}
obj1.favoritePlayers.customForEach();
&#13;
&#13;
&#13;

答案 2 :(得分:0)

遍历对象并以您认为合适的方式返回。

let playerInfo = obj1.favoritePlayers.map(playerObj => {
   return playerObj["name"] + "plays for " + playerObj["club"];
});

答案 3 :(得分:0)

尝试以下代码。

var obj1 = {
score: [10, 22, 30, 55, 70, 90],
lettersOfMyName: ["a", "m", "r"],
avgScore: null,
favoritePlayers: [
    {
        name: "messi",
        club: "barcelona"
    },

    {
        name: "totti",
        club: "roma"
    }
],
getPlaysIn:function() {
   var returnVal = "";
   this.favoritePlayers.forEach( (favoritePlayer)=> {
        returnVal += favoritePlayer.name + " plays in " + favoritePlayer.club+"\n";
        });
    return returnVal;
   }
}

console.log(obj1.getPlaysIn());

答案 4 :(得分:0)

var obj1 = {
  score: [10, 22, 30, 55, 70, 90],
  lettersOfMyName: ["a", "m", "r"],
  avgScore: null,
  favoritePlayers: [{
      name: "messi",
      club: "barcelona"
    },

    {
      name: "totti",
      club: "roma"
    }
  ]
}

var a = obj1.favoritePlayers;
for(var i = 0; i<a.length; i++){
console.log(a[i].name+ " " + "plays " +" "+ a[i].club)
};