简体   繁体   English

如何访问嵌套的JSON数组

[英]how to access nested json array

I'm trying to access data in the following array but am having trouble using .forEach I want to access all singleLine.text but it won't let me. 我正在尝试访问以下数组中的数据,但是使用.forEach时遇到问题,我想访问所有singleLine.text但它不允许我这样做。 I am guessing because there are multiple singleLines at the same level. 我猜是因为在同一级别有多个singleLines。

var a = [ 
  { boxedCharacters: { text: '1040043' } },
  { singleLine: { text: '東京都中央区日本橋新古町' } },
  { singleLine: { text: 'この度は、数多くのお店の中から当店を'} },
  { singleLine: { text: 'お選びご来店頂きまして誠にありがとう'} }
]

 a.forEach(function(value){
      console.log(value.singleLine.text)
      })

I've tried this but it isn't the best solution since it'll create an empty array for boxedCharacters because it won't match the passed in property value. 我已经尝试过了,但这不是最好的解决方案,因为它将为boxedCharacters创建一个空数组,因为它将与传入的属性值不匹配。 Also, I could have multiple boxedCharaters and other keys with the same name. 另外,我可能有多个具有相同名称的boxedCharaters和其他键。

function genNewArray(jsonArray)
 {return results.map(function(a){
 return {[jsonArray]: a[jsonArray]}
 })
}

var i = genArray(‘singleLine’)

Any better ideas of how I could access the values? 关于如何访问这些值的任何更好的想法?

$.each(a, function(entryIndex, entry) {
  $.each(this.singleLine, function() { 
    alert(this.text); 
  }); 
 });

In first case it is failing because of this console.log(value.singleLine.text) . 在第一种情况下,由于此console.log(value.singleLine.text)而失败。 Because the array also have a key named boxedCharacters and inside the forEach when the item is { boxedCharacters: { text: '1040043' } }, it will not find value.singleLine . 因为当项目为{ boxedCharacters: { text: '1040043' } },数组还具有一个名为boxedCharacterskey并且在forEach{ boxedCharacters: { text: '1040043' } },因此找不到value.singleLine so it will fail 所以会失败

 var a = [{ boxedCharacters: { text: '1040043' } }, { singleLine: { text: '東京都中央区日本橋新古町' } }, { singleLine: { text: 'この度は、数多くのお店の中から当店を' } }, { singleLine: { text: 'お選びご来店頂きまして誠にありがとう' } } ] a.forEach(function(value) { if (value.singleLine) { console.log(value.singleLine.text) } }) 

You could try the following: 您可以尝试以下方法:

  const a = [ { boxedCharacters: { text: '1040043' } }, { singleLine: { text: '東京都中央区日本橋新古町' } }, { singleLine: { text: 'この度は、数多くのお店の中から当店を'} }, { singleLine: { text: 'お選びご来店頂きまして誠にありがとう'} } ]; const getSingleLine = o => (o&&o.singleLine); const getText = o => (o&&o.text); const getSingleLineText = o => getText(getSingleLine(o)) console.log( a.map(getSingleLineText) .filter(x=>!!x)//remove undefined ) 

Or you could reduce the object to the value and if the value doesn't exist return undefined: 或者,您可以将对象减小为该值,如果该值不存在,则返回undefined:

const tryGet = keys => object =>
  keys.reduce(
    (o,key)=>
      (o!==undefined && o[key]!==undefined)
        ? o[key]
        : undefined,
    object
  );

const getSingleLineText = tryGet(["singleLine","text"]);

console.log(
  a.map(getSingleLineText)
  .filter(x=>!!x)//remove undefined
);

You just need to test whether there is value for singleLine or not.. 您只需要测试singleLine是否有价值。

a.forEach(function(value){
  if(a.singleLine) console.log(value.singleLine.text)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM