首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JavaScripts中从另一个数组创建对象的新数组

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

Stack Overflow用户
提问于 2020-11-25 13:48:08
回答 4查看 51关注 0票数 0

从一个对象数组中,我需要创建另一个带有一些字段的对象数组。

在我的organicResults数组中,我有以下几项

我只需要标题和新数组中的url。我尝试了以下几种方法

代码语言:javascript
复制
 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)

输出为空。

我哪里做错了?

EN

回答 4

Stack Overflow用户

发布于 2020-11-25 14:00:29

您需要使用return

在sitesInfo部件上插入此内容

代码语言:javascript
复制
var sitesInfo = organicResults.map(results => {
var object = {
        title: result.title,
        url : result.url,
        description: result.description
    }
    return object;
});
console.log(sitesInfo)

告诉我这对你是否有效!:)

票数 0
EN

Stack Overflow用户

发布于 2020-11-25 14:11:38

检查您的括号中的map方法..

代码语言:javascript
复制
let sitesInfo = organicResults.map(result => {
  return {
    title: result.title,
    url : result.url,
    description: result.description
  }
})
票数 0
EN

Stack Overflow用户

发布于 2020-11-25 14:19:38

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

代码语言:javascript
复制
let sitesInfo = organicResults.map(function(el) {
  return {title: el.title, url: el.url, description: el.description}
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64999131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档