本文介绍了JSON / JavaScript广告:含有一定属性数组对象回报指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于JSON对象像这样:

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"
  }

我问了一下解析对象在哪里标签的问题:在这里数:
JSON/Javascript:返回的数组对象包含某个属性

我得到了足够的答案有(使用过滤器()),但现在需要知道对象的原始索引。

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

这似乎有了答案,但我只是不要知道足够了解的JavaScript将它转化为我的特殊问题有用的东西。

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.

以下code成功返回的对象。现在,我需要修改这个返回对象的原始指数:

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;

这必须是一个纯JavaScript的解决方案,无需外部库等。

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

推荐答案

我不认为你可以修改过滤解决方案,在过滤器中,你已经失去了索引。

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.

因此​​,这里是一个纯JS的解决方案:

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()

这篇关于JSON / JavaScript广告:含有一定属性数组对象回报指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 10:41