[英]javascript function to get list of objects and return new key with new value in new array
for privacy I explain simple example.为了隐私,我解释了一个简单的例子。 function that get personal list and return new key with new value like below: function 获取个人列表并返回具有新值的新密钥,如下所示:
personalList = [{
firstName: "elena",
lastName: "zakharova"
},
{
firstName: "alex",
lastName: "farmanda"
},
{
firstName: "mike",
lastName: "kenan"
}]
output should be like this: output应该是这样的:
desiredList =[{fullname :'elena zakhrova'},{fullname :'alex farmanda'}, fullname : 'mike kenan']
tried some ways but unfortunately did not get answer尝试了一些方法但不幸的是没有得到答案
Any solutions would be appreciated.任何解决方案将不胜感激。
You can use map您可以使用 map
const personalList = [{ firstName: "elena", lastName: "zakharova" }, { firstName: "alex", lastName: "farmanda" }, { firstName: "mike", lastName: "kenan" }] console.log(personalList.map(({firstName,lastName}) => ({fullName: `${firstName} ${lastName}`})))
Here's How I Would Solve This Using For Loop.下面是我如何使用 For 循环解决这个问题。 You Can Also Use Maps Like How @R4ncid Has Explained您也可以像@R4ncid 解释的那样使用地图
var desiredlist = [] var personalList = [{ firstName: "elena", lastName: "zakharova" }, { firstName: "alex", lastName: "farmanda" }, { firstName: "mike", lastName: "kenan" }] for (i=0;i<personalList.length;i++){ desiredlist.push({"fullname":personalList[i].firstName + " " + personalList[i].lastName})} console.log(desiredlist)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.