2017-07-02 41 views
0

我有來自刮技術頁面上的以下JSON數據,我已經做了JSON數組名爲myArray,下面是:如何訪問對象在陣列節JS

{ 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

另外我是新來的。

+0

[訪問/處理(嵌套的)對象,數組或JSON(的可能的複製https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json ) –

+0

@ user3679330您已將我的答案作爲接受答案移除的任何具體原因? –

+0

我試圖讓他們都接受答案,因爲他們都做了我想要的東西 – user3679330

回答

1

循環遍歷應用程序並使用索引訪問類別。

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); 
 
    }

1

可以使用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 
 
});

1

您可以使用lodash映射函數,該函數。 https://lodash.com/docs/4.17.4#map

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

如果沒有=>,你會怎麼寫? – user3679330

+0

只是使用常規函數,lodash.map(myArray.applications,function(item){return item.categories}); –