简体   繁体   English

javascript function 获取对象列表并在新数组中返回具有新值的新键

[英]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.

相关问题 从对象数组中,返回新数组中的键 - From an array of objects, return key in new array 比较两个 Arrays 对象,如果 Javascript 中的值为真,则返回一个新数组 - Compare Two Arrays Of Objects and return a new array if value is true in Javascript 有什么方法可以使用Javascript的Array.map函数返回对象+添加到对象的新键值对 - Is there any way to use Javascript's Array.map function to return the object + new key value pair added to it 如何 map 具有新键值的新数组中的对象数组 - How to map an array of objects in a a new array with new key value 将带有值的新键添加到嵌套对象数组中 - Add new key with Value to an array of nested Objects 根据键对对象数组进行分组并返回新的对象数组 - Group array of objects based on a key and return the new array of objects 如何添加数组新键值(javascript)? - How to add array new key value ( javascript )? 在javascript数组中添加新键值不影响 - adding new key value in javascript array not affecting 如何在JavaScript中向数组添加新的键和值? - How to Add new key and value to array in javascript? 从对象数组中匹配键和值并返回仅具有该键的新 object 的正确方法是什么 - What is the proper way to match key and value from an array of objects and return a new object with only that key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM