简体   繁体   中英

How to filter json object from some objects. (Select only some values)

I need a help! I have a json object like this: (res.json(record[0].content)) I need to transform this json to another view... Select only some values... I would be grateful for any help!

[
    {
        "classId": 0,
        "type": "d",
        "cluster": -2,
        "position": 1,
        "version": 0,
        "value": {
            "@type": "d",
            "rid": "#15:0",
            "class": "Currency",
            "Name_currency": "Фунт"
        }
    },
    {
        "classId": 0,
        "type": "d",
        "cluster": -2,
        "position": 2,
        "version": 0,
        "value": {
            "@type": "d",
            "rid": "#15:1",
            "class": "Currency",
            "Name_currency": "Доллар"
        }
    },
    {
        "classId": 0,
        "type": "d",
        "cluster": -2,
        "position": 3,
        "version": 0,
        "value": {
            "@type": "d",
            "rid": "#15:2",
            "class": "Currency",
            "Name_currency": "Гривна"
        }
    }
]

How can I get this? Only values of my json object?

[
    {
        "@type": "d",
        "rid": "#15:0",
        "class": "Currency",
        "Name_currency": "Фунт"\
    },
    {
        "@type": "d",
        "rid": "#15:1",
        "class": "Currency",
        "Name_currency": "Доллар"
    },
    {
        "@type": "d",
        "rid": "#15:2",
        "class": "Currency",
        "Name_currency": "Гривна"
    }
]

Use Array.prototype.map on your backend (Node.js) site:

res.json(record[0].content.map(function(e) {
    return e.value;
}));

You should use javascript map() method (where a is your first array):

var newObj = a.map(function(obj){
    return obj.value;
});

console.log(newObj);

For more information https://developer.mozilla.org/ru/docs/JavaScript/Reference/Global_Objects/Array/map

use .each() in jquery to traverse the object

var value=[];
$.each(data,function(i,val){   
  value.push(val.value);  
});

console.log(value);

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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