1

Given a JSON object such as this:

{
  "something": {
    "terms": [
      {
        "span": [
          15,
          16
        ],
        "value": ":",
        "label": "separator"
      },
      {
        "span": [
          16,
          20
        ],
        "value": "12.5",
        "label": "number"
      }
    ],
    "span": [
      15,
      20
    ],
    "weight": 0.005,
    "value": ":12.5"
  }
 }

I asked a question about parsing the object out where label: number here: JSON/Javascript: return which array object contains a certain property

I got a sufficient answer there (use filter()), but now need to know the original index of the object.

This issue seems to have the answer, but I simply don't know enough about javascript to translate it into something useful for my particular problem.

The following code successfully returns the object. Now I need to modify this to return the original index of the object:

var numberValue, list = parsed.something.terms.filter(function(a){
  return a.label==='number';
});
numberValue = list.length ? list[0].value : -1;

This needs to be a pure javascript solution, no external libraries, etc.

2
  • 1
    What you want to acheive ? what you expect from answer ? an example please. Commented Aug 2, 2015 at 14:38
  • @ZakariaAcharki Not sure what you mean. The only thing I need returned is the index of the original object. This is requested in my question and is achieved by the approved answer. Is there something specific I needed to do in order to make this question better? Happy to do so if needed. Commented Aug 2, 2015 at 14:50

2 Answers 2

3

I don't think you can modify the filter solution as within the filter you've lost the indexes.

The solution you've linked to uses the angular external library.

So here is a pure JS solution:

var numberValue = parsed.something.terms
    .map(function(d){ return d['label']; })
    .indexOf('number');

Array.prototype.indexOf()

Array.prototype.map()

Sign up to request clarification or add additional context in comments.

4 Comments

indexOf doesn't work in IE8, which may or may not be a big deal. The OP should make sure to add this function to the array prototype.
Yeah I assumed that since filter was already being used that map/indexOf would be OK
@frenchie, well, neither does filter.
Thanks. IE8 compatibility is not needed. OP
1

Use forEach and collect the indices of objects that satisfy the a.label==='number' predicate :

var target = parsed.something.terms;
var list = [];
target.forEach(function(element, index){
    if (element.label==='number') {
        list.push(index)
    };
});
var numberValue = list.length ? target[list[0]].value : -1;

console.log(list); // [1]
console.log(numberValue); // 12.5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.