JSON在JavaScript中找到 [英] JSON find in JavaScript

查看:159
本文介绍了JSON在JavaScript中找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了在 JSON 中查找数据之外,还有更好的方法吗?这是编辑和删除。

Is there a better way other than looping to find data in JSON? It's for edit and delete.

for(var k in objJsonResp) {
  if (objJsonResp[k].txtId == id) {
    if (action == 'delete') {
      objJsonResp.splice(k,1);
    } else {
      objJsonResp[k] = newVal;
    }
    break;
  }
}

数据按地图列表排列。
赞:

The data is arranged as list of maps. Like:

[
  {id:value, pId:value, cId:value,...},
  {id:value, pId:value, cId:value,...},
  ...
]


推荐答案

(你不是在搜索JSON,而是搜索数组 - JSON字符串有已被反序列化为对象图,在本例中为数组。)

(You're not searching through "JSON", you're searching through an array -- the JSON string has already been deserialized into an object graph, in this case an array.)

一些选项:

如果你控制了这个东西的生成,它是一个数组吗?因为如果没有,有一个更简单的方法。

If you're in control of the generation of this thing, does it have to be an array? Because if not, there's a much simpler way.

说这是你的原始数据:

[
    {"id": "one",   "pId": "foo1", "cId": "bar1"},
    {"id": "two",   "pId": "foo2", "cId": "bar2"},
    {"id": "three", "pId": "foo3", "cId": "bar3"}
]

您可以执行以下操作吗?

Could you do the following instead?

{
    "one":   {"pId": "foo1", "cId": "bar1"},
    "two":   {"pId": "foo2", "cId": "bar2"},
    "three": {"pId": "foo3", "cId": "bar3"}
}

然后按ID查找相关条目是微不足道的:

Then finding the relevant entry by ID is trivial:

id = "one"; // Or whatever
var entry = objJsonResp[id];

...正在更新它:

objJsonResp[id] = /* New value */;

...并将其删除:

delete objJsonResp[id];

这利用了以下事实:在JavaScript中,您可以使用属性名称索引到对象一个字符串 - 该字符串可以是文字,也可以来自变量,如上面的 id

This takes advantage of the fact that in JavaScript, you can index into an object using a property name as a string -- and that string can be a literal, or it can come from a variable as with id above.

(愚蠢的想法,早于上述。保留历史原因。)

(Dumb idea, predates the above. Kept for historical reasons.)

看起来你需要将它作为一个数组,在这种情况下除了你想在它上面放一个地图之外没有比搜索数组更好的方法,如果你有控制权就可以做到对象的生成。例如,假设你最初有这个:

It looks like you need this to be an array, in which case there isn't really a better way than searching through the array unless you want to put a map on it, which you could do if you have control of the generation of the object. E.g., say you have this originally:

[
    {"id": "one",   "pId": "foo1", "cId": "bar1"},
    {"id": "two",   "pId": "foo2", "cId": "bar2"},
    {"id": "three", "pId": "foo3", "cId": "bar3"}
]

生成代码可以提供id-to-index映射:

The generating code could provide an id-to-index map:

{
    "index": {
        "one": 0, "two": 1, "three": 2
    },
    "data": [
        {"id": "one",   "pId": "foo1", "cId": "bar1"},
        {"id": "two",   "pId": "foo2", "cId": "bar2"},
        {"id": "three", "pId": "foo3", "cId": "bar3"}
    ]
}

然后在变量 id 中获取id的条目是微不足道的:

Then getting an entry for the id in the variable id is trivial:

var index = objJsonResp.index[id];
var obj = objJsonResp.data[index];

这利用了您可以使用属性名称索引到对象的事实。

This takes advantage of the fact you can index into objects using property names.

当然,如果你这样做,你必须在修改数组时更新地图,这可能会成为一个维护问题。

Of course, if you do that, you have to update the map when you modify the array, which could become a maintenance problem.

但是如果您无法控制对象的生成,或者更新ids-to-index的映射太多代码和/或维护问题,那么您将不得不进行暴力搜索。

But if you're not in control of the generation of the object, or updating the map of ids-to-indexes is too much code and/ora maintenance issue, then you'll have to do a brute force search.

有点OT(虽然你做了询问是否有是一个更好的方式:-)),但循环数组的代码是不正确的。 此处详细信息,但您无法使用 for..in 循环遍历数组索引(或者更确切地说,如果你这样做,你必须特别努力); for..in 循环遍历对象的属性,而不是数组的索引。使用非稀疏数组(和你的非稀疏数组)最好的选择是标准的老式循环:

Somewhat OT (although you did ask if there was a better way :-) ), but your code for looping through an array is incorrect. Details here, but you can't use for..in to loop through array indexes (or rather, if you do, you have to take special pains to do so); for..in loops through the properties of an object, not the indexes of an array. Your best bet with a non-sparse array (and yours is non-sparse) is a standard old-fashioned loop:

var k;
for (k = 0; k < someArray.length; ++k) { /* ... */ }

var k;
for (k = someArray.length - 1; k >= 0; --k) { /* ... */ }

无论你喜欢什么(后者在所有实现中并不总是更快,这对我来说是违反直觉的,但我们确实如此)。 (使用稀疏数组,您可以使用 for..in 但是再次采取特殊措施以避免陷阱;在上面链接的文章中有更多内容。)

Whichever you prefer (the latter is not always faster in all implementations, which is counter-intuitive to me, but there we are). (With a sparse array, you might use for..in but again taking special pains to avoid pitfalls; more in the article linked above.)

在数组上使用 for..in 似乎可以在简单的情况下工作,因为数组具有每个索引的属性,并且它们唯一的其他默认属性( length 及其方法)被标记为不可枚举。但是一旦你设置(或框架设置)数组对象上的任何其他属性(它完全有效;数组只是在长度 property)。

Using for..in on an array seems to work in simple cases because arrays have properties for each of their indexes, and their only other default properties (length and their methods) are marked as non-enumerable. But it breaks as soon as you set (or a framework sets) any other properties on the array object (which is perfectly valid; arrays are just objects with a bit of special handling around the length property).

这篇关于JSON在JavaScript中找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆