【问题标题】:How to access Object in Array NODE JS如何访问 Array NODE JS 中的对象
【发布时间】:2017-12-05 16:11:04
【问题描述】:

我有以下来自页面上抓取技术的 JSON 数据,我制作了一个名为 MyArray 的 Json 数组,如下所示:

{ url: 'http://www.ozautoelectrics.com',
originalUrl: 'http://www.ozautoelectrics.com',
applications:
[ { name: 'Font Awesome',
   confidence: '100',
   version: '',
   icon: 'Font Awesome.png',
   website: 'http://fontawesome.io',
   categories: [Object] },
 { name: 'Google Analytics',
   confidence: '100',
   version: '',
   icon: 'Google Analytics.svg',
   website: 'http://google.com/analytics',
   categories: [Object] },
 { name: 'jQuery',
   confidence: '100',
   version: '2.1.3',
   icon: 'jQuery.svg',
   website: 'http://jquery.com',
   categories: [Object] } ] }

我的问题是如何使用 NODE 访问类别中的 [Object]?或应用程序中的任何其他内容?

我可以使用 myArray.url 获取 URL,但是如何正确获取应用程序内部的内容?我试过 myArray.applications.name

我也是新人。

【问题讨论】:

  • @user3679330 您将我的答案作为已接受答案删除的任何具体原因?
  • 我试图让他们都接受答案,因为他们都做了我想做的事
  • @user3679330 不,您不能接受所有答案。你只能接受一个能解决你问题的人。如果您接受其他答案意味着它将恢复已接受的答案。
  • 你可以得到接受的答案,我觉得你更想要它

标签: arrays node.js object


【解决方案1】:

您可以为此使用 lodash 地图功能。 https://lodash.com/docs/4.17.4#map

var lodash = require('lodash');
var categoriesArr = lodash.map(myArray.applications, (item) => item.categories);

【讨论】:

  • 如果没有 => 怎么写?
  • 只使用常规函数,lodash.map(myArray.applications, function(item) {return item.categories});
【解决方案2】:

您可以使用forEach 方法访问数组的每个元素。

var myArray = { 
url: 'http://www.ozautoelectrics.com',
originalUrl: 'http://www.ozautoelectrics.com',
applications: [ { name: 'Font Awesome',
   confidence: '100',
   version: '',
   icon: 'Font Awesome.png',
   website: 'http://fontawesome.io',
   categories: [Object] },
 { name: 'Google Analytics',
   confidence: '100',
   version: '',
   icon: 'Google Analytics.svg',
   website: 'http://google.com/analytics',
   categories: [Object] },
 { name: 'jQuery',
   confidence: '100',
   version: '2.1.3',
   icon: 'jQuery.svg',
   website: 'http://jquery.com',
   categories: [Object] } 
   ]};
   
myArray.applications.forEach(function(value, index, array){
  console.log(value.categories); //Allow to access the array
  //console.log(value.categories[0]); //Allows the first element in the array
});


console.log('Arrow function Solution');
//solution with arrow function
myArray.applications.forEach((value, index, array) => {
  console.log(value.categories); //Allow to access the array
  //console.log(value.categories[0]); //Allows the first element in the array
});

【讨论】:

    【解决方案3】:

    循环应用程序并使用索引访问类别。

    var myArray = { url: 'http://www.ozautoelectrics.com',
    originalUrl: 'http://www.ozautoelectrics.com',
    applications:
    [ { name: 'Font Awesome',
       confidence: '100',
       version: '',
       icon: 'Font Awesome.png',
       website: 'http://fontawesome.io',
       categories: [Object] },
     { name: 'Google Analytics',
       confidence: '100',
       version: '',
       icon: 'Google Analytics.svg',
       website: 'http://google.com/analytics',
       categories: [Object] },
     { name: 'jQuery',
       confidence: '100',
       version: '2.1.3',
       icon: 'jQuery.svg',
       website: 'http://jquery.com',
       categories: [Object] } ] 
       };
       
       for(var i =0; i<myArray.applications.length;i++){
        console.log(myArray.applications[i].categories);
       }

    【讨论】:

      猜你喜欢
      • 2020-06-01
      • 1970-01-01
      • 2023-01-09
      • 2022-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      相关资源
      最近更新 更多