1

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. I am guessing because there are multiple singleLines at the same level.

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. Also, I could have multiple boxedCharaters and other keys with the same name.

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?

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
Max
  • 60
  • 6
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Teemu Apr 27 '18 at 16:25
  • 1
    Your first example is fine. You just have to account for the fact that not every object has a `singleLine` property. – Felix Kling Apr 27 '18 at 16:27
  • 1
    Also, this isn't JSON, it's a JavaScript array. Nothing about your question is related to JSON. – Felix Kling Apr 27 '18 at 16:28
  • @FelixKling ah, yes thanks. The actual data I am hanlding is a larger json but I just pulled the specific part I was having problem with. – Max Apr 27 '18 at 16:31

4 Answers4

0
$.each(a, function(entryIndex, entry) {
  $.each(this.singleLine, function() { 
    alert(this.text); 
  }); 
 });
parlad
  • 1,143
  • 4
  • 23
  • 42
0

In first case it is failing because of this 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. 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)
  }

})
brk
  • 48,835
  • 10
  • 56
  • 78
  • Actually, when I do this it works. So I don't think it's because of boxedCharacters being the problem but thanks for the answer. Really helped me better understand processing arrays a.forEach(function(value){ console.log(value.singleLine) }) – Max Apr 30 '18 at 09:57
0

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:

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
);
HMR
  • 37,593
  • 24
  • 91
  • 160
0

You just need to test whether there is value for singleLine or not..

a.forEach(function(value){
  if(a.singleLine) console.log(value.singleLine.text)
})
Incepter
  • 2,711
  • 15
  • 33