如何从javascript中具有特定值的json数组/对象中选择字段? [英] how to select the fields from an json array/object having particular value in javascript?

查看:123
本文介绍了如何从javascript中具有特定值的json数组/对象中选择字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JOSN对象{显示:{FIRST_NAME:X,MID_NAME:X,LAST_NAME:X,LAST_CHANGED:X,ADP_FILE :NA,MARITAL:NA,EXEMP:NA}}



我只想要上面带有值X的字段JSON对象。

解决方案

这个只想要不是你需要的规范。这是什么意思?未定义部分属性?创建一个具有相同属性的新JavaScript对象(不是字符串),除了具有除X之外的值的那些属性外?你能行的。什么?



无论如何,你可以使用:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON [ ^ ],

从JSON字符串创建结构: https://developer.mozilla .org / zh-CN / docs / Web / JavaScript / Reference / Global_Objects / JSON / parse [ ^ ],

遍历循环中的任何对象:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/声明/对_each ...在 [ ^ ],

以及如何从对象中删除属性: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete [ ^ ]。



您可以创建一个新对象并在循环中创建新属性;

  var  result = {}; 
// ...
result.LAST_NAME = X; // sic!





JavaScript对象(包括数组)是关联容器;您可以随时添加任何属性,并且属性查找具有O(1)的时间复杂度

http://en.wikipedia.org/wiki/Associative_array [ ^ ],

http://en.wikipedia .org / wiki / JavaScript #Dynamic [ ^ ];

另见: http://en.wikipedia.org/wiki/Big_O_notation [ ^ ],

http://en.wikipedia.org/wiki/Time_complexity [ ^ ]。



SA

I am having an JOSN object {"Display":{"FIRST_NAME":"X","MID_NAME":"X","LAST_NAME":"X","LAST_CHANGED":"X","ADP_FILE":"NA","MARITAL":"NA","EXEMP":"NA"}}

I want only the fields having value "X" form above JSON object.

解决方案

This "want only" in not a specification of what you need. What would it mean? Undefine part of the properties? Create a new JavaScript object (not string) with the same properties except those with the values other then "X"? You can do it. What?

Anyway, you can use:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON[^],
create a structure from JSON string: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse[^],
traverse any object in a loop: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for_each...in[^],
and this is how you can remove a property from an object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete[^].

You can create a new object and create new properties in a loop;

var result = {};
// ...
result.LAST_NAME = "X"; //sic!



JavaScript objects (including arrays) are associative containers; you can add any property at any time, and the property lookup has time complexity of O(1):
http://en.wikipedia.org/wiki/Associative_array[^],
http://en.wikipedia.org/wiki/JavaScript#Dynamic[^];
see also: http://en.wikipedia.org/wiki/Big_O_notation[^],
http://en.wikipedia.org/wiki/Time_complexity[^].

—SA


这篇关于如何从javascript中具有特定值的json数组/对象中选择字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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