简体   繁体   English

如何使用Jquery的Javascript从具有不同格式的嵌套JSON中提取值?

[英]How to extract values from nested JSON with different formatting using Javascript of Jquery?

var cart =
 [
 {
    "Items": "",
    "category": "",
    "contents":
 [
 {
    "Apple iPhone": "222",
    "French": "Bounjour",
    "id": 1234,
    "icon": "/images/bg.jpg",
    "callback": "use()",
    "pricetag":"false"
 }
 ]
 }, 
 {
    "Items": "No 2",
    "category": "2nd",
    "contents":
 [
 {
    "redmi": "333",
    "French": "some text",
    "id": 345787,
    "icon": "/images/bg.jpg",
    "callback": "use()",
    "pricetag":"true"
 }, 
 {
    "samsung": "333",
    "French": "some text",
    "id": 86787876,
    "icon": "/images/bg.jpg",
    "callback": "use()",
    "pricetag":"disabled"
 }
 ]
 }
 ];

Can anyone help me to get the "id" value and "pricetag" value from the above JSON ? 谁能帮助我从上述JSON获取“ id”值和“ pricetag”值? It is nested one and can be in different format. 它是嵌套的,可以采用不同的格式。 But in each, i need to extract id value and pricetag value. 但是在每一个中,我需要提取id值和pricetag值。 I tried so many things but not getting exact output. 我尝试了很多事情,但没有得到确切的输出。 Can someone please help me ? 有人可以帮帮我吗 ? Sorry for the bad formatting .. 对不起,格式错误。

use .map() to iterate and return the desired keys : 使用.map()进行迭代并返回所需的键:

 var cart = [{ "Items": "", "category": "", "contents": [{ "Apple iPhone": "222", "French": "Bounjour", "id": 1234, "icon": "/images/bg.jpg", "callback": "use()", "pricetag": "false" }] }, { "Items": "No 2", "category": "2nd", "contents": [{ "redmi": "333", "French": "some text", "id": 345787, "icon": "/images/bg.jpg", "callback": "use()", "pricetag": "true" }, { "samsung": "333", "French": "some text", "id": 86787876, "icon": "/images/bg.jpg", "callback": "use()", "pricetag": "disabled" } ] } ]; let values = cart.map((e) => { return e.contents.map((a) => { return { id: a.id, pricetag: a.pricetag } }) }) console.log(values) 

This should work for you : 这应该为您工作:

   var items=[];
   var some_var;
   const map1 = cart.map(function(item){

     const map2=item.contents.map(function(contentItem){
        //some_var=contentItem.id;
       return{
          pricetag:contentItem.pricetag,
          id:contentItem.id
        }
     })
     items=items.concat(map2)
 });

console.log(items);

Sample output: 样本输出:

[ { pricetag: 'false', id: 1234 },
  { pricetag: 'true', id: 345787 },
  { pricetag: 'disabled', id: 86787876 } ]

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

相关问题 如何从 javascript 中的 json 中提取 json 值 - How to extract json values from json in javascript 如何从 json 中提取所有键和值? 使用 Javascript - How to extract all keys and values from json? using Javascript 如何从嵌套在 object 内的数组中的 object 中提取值并将其存储在不同的 arrays Z53C6C951F4E558B9DFF298869 - how to extract values from an object nested inside array inside object and store it in different arrays JAVASCRIPT 使用JS从嵌套的JSON中提取值到数组 - Extract values from nested JSON to array using JS 使用Javascript从Woocommerce JSON webhook提取嵌套对象 - Extract nested objects from Woocommerce JSON webhook using Javascript 如何通过从单独的数组中匹配的字符串对象值从 3 个嵌套的 json 数组中提取对象值,然后在 html/javascript 中显示它 - How can I extract object values from 3 nested json arrays by matched string object values from a separate array and then display it in html/javascript 从javascript中的json提取值 - Extract values from json in javascript 如何根据嵌套值从 json 文件中提取对象(使用 JavaScript) - How do I extract an object from a json file based on a nested value (using JavaScript) 如何使用Javascript从JSON提取值? - How to extract a value from JSON using Javascript? 如何使用 javascript 在 html 表行中提取嵌套的 json 值 - How to extract nested json value in a html table row using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM