简体   繁体   English

不使用循环仅提取 JSON object 中的值 javascript

[英]Extract only values from JSON object in javascript without using a loop

is there a "Nice" way to get all the values out of a json object (I don't care about the keys) - just get the values into array, without using a loop?有没有一种“不错”的方法可以从 json object 中获取所有值(我不关心键)——只需将值放入数组中,而不使用循环? (lang is Javascript) (lang 是 Javascript)

It depends on how you define "loop".这取决于您如何定义“循环”。

You can extract the properties with Object.keys and then map them to their values.您可以使用Object.keys提取属性,然后map它们map到它们的值。

… it's still essentially a loop under the hood though. ……但它本质上仍然是一个引擎盖下的循环。

 var json = `{ "foo": 1, "bar": 2, "baz": 3 }`; var obj = JSON.parse(json); var values = Object.keys(obj).map(function (key) { return obj[key]; }); console.log(values);

With weaker browser support you could use the values method.如果浏览器支持较弱,您可以使用values方法。

 var json = `{ "foo": 1, "bar": 2, "baz": 3 }`; var obj = JSON.parse(json); var values = Object.values(obj); console.log(values);

I think you are looking for Object.values() function, just pass the object to the values method of Object as first param.我认为您正在寻找 Object.values() 函数,只需将对象作为第一个参数传递给 Object 的values方法。 That's it!就是这样!

Object.values({something: 'lol'});
> ["lol"]

Recursively extract as text递归提取为文本

Yes, this is a loop but the underlying methods you are calling such as Object.values or arr.map are still loops .是的,这是一个循环,但您正在调用的基础方法(例如Object.valuesarr.map仍然是 loops I found this useful for extracting text out of a json object for full text search in particular and thought it useful as I came here initially needing this but the answers only touched the surface as json is recursive in nature.我发现这对于从 json 对象中提取文本尤其用于全文搜索很有用,并认为它很有用,因为我最初需要它来这里,但由于json本质上是递归的,因此答案仅触及表面。

function textFromJson(json) {
    if (json === null || json === undefined) {
      return '';
    }
    if (!Array.isArray(json) && !Object.getPrototypeOf(json).isPrototypeOf(Object)) {
      return '' + json;
    }
    const obj = {};
    for (const key of Object.keys(json)) {
        obj[key] = textFromJson(json[key]);
    }
    return Object.values(obj).join(' ');
}

With ES2017 you have Object.values().在 ES2017 中,你有 Object.values()。 You can polyfill it also .你也可以polyfill 它

Only you need is transform JSON to JavaScript object and call Object.values().您只需要将 JSON 转换为 JavaScript 对象并调用 Object.values()。 The result is an array of values.结果是一个值数组。

var obj = JSON.parse(jsonData);
var result = Object.values(obj);

If you pass a function to JSON.parse , it will be called each time a value is parsed:如果将 function 传递给JSON.parse ,每次解析值时都会调用它:

function get_values(json) {
   let values = []
   JSON.parse(json, (key,value)=>{ values.push(value) })
   return values
}

ex:前任:

get_values(`{"a":1, "b":[true, false], "c":3}`)
// returns a list of:
• 1
• true
• false
• [true, false]
• 3
• {a:1, b:[true, false], c:3}

Note: If you don't consider the full object to be a "value", just remove the last item.注意:如果您不认为完整的 object 是一个“值”,只需删除最后一项即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM