2016-09-26 88 views
0

如何獲取嵌套在JSON值中的數組的值以及嵌套在數組中的JSON?我怎樣才能得到嵌套在JSON值和JSON嵌套在數組中的數組值?

var contacts = [ 
     { 
      "firstName": "Akira", 
      "lastName": "Laine", 
      "number": "0543236543", 
      "likes": ["Pizza", "Coding", "Brownie Points"] 
     }, 
     { 
      "firstName": "Harry", 
      "lastName": "Potter", 
      "number": "0994372684", 
      "likes": ["Hogwarts", "Magic", "Hagrid"] 
     }, 
     { 
      "firstName": "Sherlock", 
      "lastName": "Holmes", 
      "number": "0487345643", 
      "likes": ["Intriguing Cases", "Violin"] 
     }, 
     { 
      "firstName": "Kristian", 
      "lastName": "Vos", 
      "number": "unknown", 
      "likes": ["Javascript", "Gaming", "Foxes"] 
     } 
    ]; 

/**/

,如果我想喜歡& 「名字」 的價值: 「晃」,我該怎麼辦? 有人可以幫忙嗎?

 var firstName = "Akira", prop = "likes"; 
     for (var i = 0; i < contacts.length; i++) { 
      var a; 
      console.log("contacts[i].firstName = "+contacts[i].firstName); 
      if(contacts[i].firstName == firstName){ 
       a = true; 
      }else{ 
       a = false; 
      } 
     //get the cales of likes? 
     console.log("contacts[i].prop[i].value() = "+contacts[i].prop[i].value()); 
     //contacts[i].prop[i].value(); ? 
     } 
+1

問題是什麼?你的代碼似乎沒問題。你有'a = true'的地方,你可以去'contacts.likes'或'contacts [i] [prop]'來獲得數組。 – Shilly

+0

https://gist.github.com/xgqfrms-GitHub/78fbe4ef38ee1e50965c1558a5b6245f – xgqfrms

+0

https://github.com/gildata/RAIO/issues/61 – 2017-08-10 06:57:42

回答

0

contacts數組中的對象沒有他的名字是prop的屬性。幸運的是,您可以通過名稱訪問對象上的屬性,這類似於從數組中獲取值的方式,因此如果使用contacts[i][prop],它將獲得名稱爲變量值prop的屬性值,該值位於這種情況是"likes"。由於likes也是一個數組,因此您可以通過索引contacts數組的索引編制索引來獲取數值。

var firstName = "Akira", prop = "likes"; 
for (var i = 0; i < contacts.length; i++) { 
    var a; 
    console.log("contacts[" + i + "].firstName = " + contacts[i].firstName); 
    if (contacts[i].firstName == firstName){ 
     a = true; 
    } else { 
     a = false; 
    } 

    // get the likes array and loop through to print the values 
    // you might want to put this where a = true; is if you only want to get 
    // the likes where the firstName matches 
    var likes = contacts[i][prop]; 
    for (var j = 0; j < likes.length; j++) { 
     console.log("contacts[" + i + "]." + prop + "[" + j + "] = " + likes[j]); 
    } 
} 
0

柳可能做如下;

var contacts = [ 
 
     { 
 
      "firstName": "Akira", 
 
      "lastName": "Laine", 
 
      "number": "0543236543", 
 
      "likes": ["Pizza", "Coding", "Brownie Points"] 
 
     }, 
 
     { 
 
      "firstName": "Harry", 
 
      "lastName": "Potter", 
 
      "number": "0994372684", 
 
      "likes": ["Hogwarts", "Magic", "Hagrid"] 
 
     }, 
 
     { 
 
      "firstName": "Sherlock", 
 
      "lastName": "Holmes", 
 
      "number": "0487345643", 
 
      "likes": ["Intriguing Cases", "Violin"] 
 
     }, 
 
     { 
 
      "firstName": "Kristian", 
 
      "lastName": "Vos", 
 
      "number": "unknown", 
 
      "likes": ["Javascript", "Gaming", "Foxes"] 
 
     } 
 
    ], 
 
    getMeObj = (d,p,v,t) => d.filter(o => o[p] === v)[0][t]; 
 
    console.log(getMeObj(contacts,"firstName", "Akira", "likes"));

0

下面的代碼將工作

var firstName = "Akira", prop = "likes"; 
    for(var i in contacts) 
    { 
     if(contacts[i].firstName == firstName){ 
     var likes = contacts[i][prop]; 
     console.log(likes); 
    } 
0

我有固定的代碼。如果你需要更多的細節問我,但我認爲代碼是不夠

var contacts = [{ 
    "firstName": "Akira", 
    "lastName": "Laine", 
    "number": "0543236543", 
    "likes": ["Pizza", "Coding", "Brownie Points"] 
}, { 
    "firstName": "Harry", 
    "lastName": "Potter", 
    "number": "0994372684", 
    "likes": ["Hogwarts", "Magic", "Hagrid"] 
}, { 
    "firstName": "Sherlock", 
    "lastName": "Holmes", 
    "number": "0487345643", 
    "likes": ["Intriguing Cases", "Violin"] 
}, { 
    "firstName": "Kristian", 
    "lastName": "Vos", 
    "number": "unknown", 
    "likes": ["Javascript", "Gaming", "Foxes"] 
}]; 
var firstName = "Akira" 
    , prop = "likes"; 
for (var i = 0; i < contacts.length; i++) { 
    var a; 
    console.log("contacts[" + i + "].firstName = " + contacts[i].firstName); 
    if (contacts[i].firstName == firstName) { 
    a = true; 
    } else { 
    a = false; 
    } 
    for (var n = 0; n < contacts[i].likes.length; n++) { 
    console.log("contacts[" + i + "].likes[" + n + "] = " + contacts[i].likes[n]); 
    } 
} 

簡單作爲一個邊注:答案應該是提問者的相同水平,以幫助學習:)

+0

這個問題似乎是問如何得到一個財產,當你只有一個是變量與它的屬性名稱,在這種情況下'var prop =「likes」'。這個答案是直接獲取'likes'屬性而不使用變量'prop',所以我不確定它是否真的回答了這個問題。 (只是我和其他幾個人所做的假設,很可能你是那個回答實際問題的人) – jonhopkins