为了账号安全,请及时绑定邮箱和手机立即绑定

从另一个数组在 JavaScript 中创建新的对象数组

从另一个数组在 JavaScript 中创建新的对象数组

幕布斯7119047 2023-06-15 10:04:00
从一个对象数组,我需要创建另一个包含一些字段的对象数组。在我的 organicResults 数组中,我有以下项目我只需要新数组中的标题和 url。我尝试了以下 const organicResults = [ {      title: '21 Excel Tips and Tricks to Boost Business | Process Street ...',      url: 'https://www.process.st/excel-tips-and-tricks/',      displayedUrl: 'www.process.st › excel-tips-and-tricks',      description: 'Mar 10, 2017 — 90% of businesses use Excel in their operations. Learn these Excel tips and tricks to maximize your efficiency and automate your processes!',      siteLinks: [],      productInfo: {}    },    {      title: 'Microsoft Excel Tips & Tricks | The Training Lady',      url: 'https://www.thetraininglady.com/microsoft-excel/',      displayedUrl: 'www.thetraininglady.com › microsoft-excel',      description: 'When you are entering data in Excel you may want to ensure your data is entered in a consistent way. Maybe you have multiple people working on the same file ...',      siteLinks: [],      productInfo: {}    },    {      title: 'What are the best Microsoft Excel tips according to you? - Quora',      url: 'https://www.quora.com/What-are-the-best-Microsoft-Excel-tips-according-to-you',      displayedUrl: 'www.quora.com › What-are-the-best-Microsoft-Excel-tips...',      description: 'Originally Answered: What is the best microsoft excel tip according to you? Below is a listing of all the major shortcut keys usable in Microsoft Excel.64 answers',      siteLinks: [],      productInfo: {}    },]    let sitesInfo = organicResults.map(result => ({        title: result.title,        url : result.url,        description: result.description            }))    console.log(sitesInfo)输出为空。我哪里做错了?
查看完整描述

4 回答

?
函数式编程

TA贡献1807条经验 获得超9个赞

您的代码工作正常。让我展示一个较短的版本,您可以在其中声明所需的属性:


let sitesInfo = organicResults.map(({title, url, description}) => 

    ({title, url ,description}))

一个例子:


const organicResults = [ {

      title: '21 Excel Tips and Tricks to Boost Business | Process Street ...',

      url: 'https://www.process.st/excel-tips-and-tricks/',

      displayedUrl: 'www.process.st › excel-tips-and-tricks',

      description: 'Mar 10, 2017 — 90% of businesses use Excel in their operations. Learn these Excel tips and tricks to maximize your efficiency and automate your processes!',

      siteLinks: [],

      productInfo: {}

    },

    {

      title: 'Microsoft Excel Tips & Tricks | The Training Lady',

      url: 'https://www.thetraininglady.com/microsoft-excel/',

      displayedUrl: 'www.thetraininglady.com › microsoft-excel',

      description: 'When you are entering data in Excel you may want to ensure your data is entered in a consistent way. Maybe you have multiple people working on the same file ...',

      siteLinks: [],

      productInfo: {}

    },

    {

      title: 'What are the best Microsoft Excel tips according to you? - Quora',

      url: 'https://www.quora.com/What-are-the-best-Microsoft-Excel-tips-according-to-you',

      displayedUrl: 'www.quora.com › What-are-the-best-Microsoft-Excel-tips...',

      description: 'Originally Answered: What is the best microsoft excel tip according to you? Below is a listing of all the major shortcut keys usable in Microsoft Excel.64 answers',

      siteLinks: [],

      productInfo: {}

    },

]


    let sitesInfo = organicResults.map(({title, url, description}) => 

        ({title, url , description}))

    console.log(sitesInfo)


查看完整回答
反对 回复 2023-06-15
?
HUH函数

TA贡献1836条经验 获得超4个赞

检查您的括号中的地图方法..


let sitesInfo = organicResults.map(result => {

  return {

    title: result.title,

    url : result.url,

    description: result.description

  }

})


查看完整回答
反对 回复 2023-06-15
?
largeQ

TA贡献2039条经验 获得超7个赞

使用常规函数仍然更安全。


let sitesInfo = organicResults.map(function(el) {

  return {title: el.title, url: el.url, description: el.description}

})


查看完整回答
反对 回复 2023-06-15
?
慕少森

TA贡献2019条经验 获得超9个赞

你需要使用return


将其插入 sitesInfo 部分


var sitesInfo = organicResults.map(results => {

var object = {

        title: result.title,

        url : result.url,

        description: result.description

    }

    return object;

});

console.log(sitesInfo)

告诉我这对你有用吗!:)


查看完整回答
反对 回复 2023-06-15
  • 4 回答
  • 0 关注
  • 135 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信