-1

I have json with several values. I can access the last value with json[json.length -1].ADESC.

Problem is, that the last value, or several last values can be '...'. I need to get the last value other than this '...'.

here is an example of the data:

start
...
working
...
...
...

In this case, I need to get the 'working'. There can be any rows with '...' or there can be many last rows with '...'.

I tried this:

if( json[json.length -1].ADESC != '...' ) {
    json[json.length -1].ADESC
} else if ( json[json.length -2].ADESC != '...' ) {
    json[json.length -2].ADESC
} else if ( json[json.length -3].ADESC != '...' ) {
    json[json.length -3].ADESC
} else if 
...

but it is not the best way how to accomplish this task I think and I'm afraid the better solution is out of my skills. Thank you.

I tried to add for loop. There is no error when it's executed, but unfortunately no data at all :) I have a little problem to create this reverse loop :(

for(var i=-1; i<(-(json[json.length])); i--){ 
    if(json[json.length -i].ADESC != '. . .') { 
        json[json.length -i].ADESC; } }
culter
  • 5,487
  • 15
  • 54
  • 66
  • 1
    _"I have json with several values"_ - No, you don't. According to your example it is an array of objects ([What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation)). – Andreas Feb 11 '20 at 12:29
  • Loop over the collection from the last to the first element. Stop on the first where `ADESC` is not `...` – Andreas Feb 11 '20 at 12:30
  • Thanks Andreas, you're right, I'm no developer but sysop. I edited the original post with for loop. Could you please review it? – culter Feb 11 '20 at 13:05
  • You really should grab a tutorial first ;) `'. . .' !== '...'`, `json[json.length]` is `undefined` (arrays start at index zero), `-undefined` is `NaN`, ` < NaN` will always be `false` – Andreas Feb 11 '20 at 13:25

3 Answers3

1

First filter the values then you use it like below.

const json = [ {ADESC: 'foo' }, {ADESC: '...' }, {ADESC: 'bar' }, {ADESC: '...' }, {ADESC: '...' }];

const items = json.filter(item => item.ADESC !== '...');

console.log(items[items.length -1].ADESC)
Siva K V
  • 10,561
  • 2
  • 16
  • 29
1

See result here https://runkit.com/embed/pihgsr9bqg21

const json = [ {"ADESC": "..." }, {"ADESC": "2" }, {"ADESC": "3" }, 
               {"ADESC": "..."} ,{"ADESC": "6" },{"ADESC": "..." }];
const values = json.map(item => item.ADESC)        //Get the values 
                   .filter(item => item !=="...")  //Remove the unused '...'
                   .splice(-1)[0];                 //Get the last one, "6"

Update: to avoid extracting all item values, use reduceRight:

Live code: https://runkit.com/embed/v8yzwx91u1ai

const json = [ {"ADESC": "..." }, {"ADESC": "2" }, {"ADESC": "3" }, 
               {"ADESC": "..."} ,{"ADESC": "6" },{"ADESC": "..." }];

const values = json.reduceRight( (acc, cur) => {
   return acc ? acc :  (cur.ADESC !== "..."? cur.ADESC : null);
}, null); 
David
  • 15,894
  • 22
  • 55
  • 66
  • Why should OP iterate over the _complete_ array to extract the value of every `ADESC` property to then remove elements that are equal to `...`? – Andreas Feb 11 '20 at 12:41
  • OP is obviously a beginner, so you really should add at least the links to the used methods (imho) – Andreas Feb 11 '20 at 12:42
  • @Andreas, answer updated, link updated. reduceRight might be useful to the OP. – David Feb 11 '20 at 13:02
0

Thank you all for answers, commenting. Here is the solution I figured out and which perfectly fits my needs. As I mentioned, I had a problem with a reverse loop. When I found reverse(), it made things much easier for me.

json.reverse();
for(var i = 0; i < json.length; i++){ 
    if(json[i].ADESC != '. . .'){ 
          json[i].ADESC; 
          break;
    }
}
culter
  • 5,487
  • 15
  • 54
  • 66
  • 1
    `for (let i = json.length - 1; i >= 0; i--) { ... }` (and please rename the variable. The content is _not_ [JSON](https://www.json.org/json-en.html)) – Andreas Feb 11 '20 at 16:06