如何在Json Array中获取值并分别使用它们

时间:2022-06-01 20:04:24

I have a JSON and I Want to separate each field of that in array part and then I want to use of each field separately and put them in separate array.

我有一个JSON,我想分离数组部分中的每个字段,然后我想分别使用每个字段并将它们放在单独的数组中。

for example I have two part in my array and I want to divide the first part too room 1 and second part in room 2 .

例如,我在我的阵列中有两个部分,我想将第一部分分为房间1和第二部分在房间2中。

I have to send Json to my second page with format room1 and room 2. and I do not know how can I do this

我必须将Json发送到我的第二页,格式为room1和room 2.我不知道怎么能这样做

my json is like this right now :

我的json现在是这样的:

"rooms": [
    {
      "adultcount": "1",
      "childcount": "1,1"
    },
    {
      "adultcount": "1",
      "childcountandage": "0 "
    }
]

but I want to change it like this :

但我想改变它:

"rooms": [
    {
        "rooms1": {
            "adultcount": "1",
            "childcount": "1,1"
        }
    },
    {
        "rooms2": {
            "adultcount": "2",
            "childcount": "10,1"
        }
    }
]

then I need to use them. how can I do this with jquery ?

然后我需要使用它们。我怎么能用jquery做到这一点?

there is no need to change the json code I just wrote the sample the new json to define better.

没有必要更改json代码我只是编写了新的json样本以更好地定义。

here is my code :

这是我的代码:

$( document ).ready(function() {
var research={"rooms":[{ "adultcount":"1","childcount":"1,1" },{ "adultcount":"1","childcountandage":"0 " }] }
var adultcount = research.rooms[0].adultcount;
var childcount = research.rooms[0].childcount;

});

3 个解决方案

#1


4  

Since you have an array that you want to make into an object and the property name seems to be the index inside the array, you can use a basic array.reduce:

由于您有一个要在对象中创建的数组,并且属性名称似乎是数组中的索引,因此您可以使用基本的array.reduce:

var rooms = [
    { "adultcount":"1", "childcount":"1,1" },
    { "adultcount":"2", "childcount":"10,1" }
];

var roomsMap = rooms.reduce( function( map, room, index ) {
    map[ 'room' + ( index + 1 ) ] = room;
    return map;
}, {} );

var otherRoomsMap = rooms.map( function( room, index ) {
    var wrapper = {};
    wrapper[ 'room' + ( index + 1 ) ] = room;
    return wrapper;
} );

console.log( roomsMap );

console.log( otherRoomsMap );

edit:

编辑:

I have added the other example of keeping the array and just wrapping the objects isnide another object, but I have no idea what advantage that would give over the original array.

我添加了保持数组的另一个例子,只是包装对象是另一个对象,但我不知道它会给原始数组带来什么好处。

#2


0  

You can access your json array using loop

您可以使用循环访问您的json数组

$.each(research, function (key, value) {
    var adultcount = value.adultcount;
    var childcount = value.childcount;
    console.log("Adult count is:"+value.adultcount);
    console.log("Child count is:"+value.childcount);
});

#3


0  

Try this:

尝试这个:

var research={"rooms":[{ "adultcount":"1","childcount":"1,1" },{"adultcount":"1","childcountandage":"0 " }] };

var newResearch = {"rooms": []};

research.rooms.forEach(function(r) {
    newResearch.rooms.push({"room1": r[0], "room2": r[1]});
});

console.log(newResearch);

#1


4  

Since you have an array that you want to make into an object and the property name seems to be the index inside the array, you can use a basic array.reduce:

由于您有一个要在对象中创建的数组,并且属性名称似乎是数组中的索引,因此您可以使用基本的array.reduce:

var rooms = [
    { "adultcount":"1", "childcount":"1,1" },
    { "adultcount":"2", "childcount":"10,1" }
];

var roomsMap = rooms.reduce( function( map, room, index ) {
    map[ 'room' + ( index + 1 ) ] = room;
    return map;
}, {} );

var otherRoomsMap = rooms.map( function( room, index ) {
    var wrapper = {};
    wrapper[ 'room' + ( index + 1 ) ] = room;
    return wrapper;
} );

console.log( roomsMap );

console.log( otherRoomsMap );

edit:

编辑:

I have added the other example of keeping the array and just wrapping the objects isnide another object, but I have no idea what advantage that would give over the original array.

我添加了保持数组的另一个例子,只是包装对象是另一个对象,但我不知道它会给原始数组带来什么好处。

#2


0  

You can access your json array using loop

您可以使用循环访问您的json数组

$.each(research, function (key, value) {
    var adultcount = value.adultcount;
    var childcount = value.childcount;
    console.log("Adult count is:"+value.adultcount);
    console.log("Child count is:"+value.childcount);
});

#3


0  

Try this:

尝试这个:

var research={"rooms":[{ "adultcount":"1","childcount":"1,1" },{"adultcount":"1","childcountandage":"0 " }] };

var newResearch = {"rooms": []};

research.rooms.forEach(function(r) {
    newResearch.rooms.push({"room1": r[0], "room2": r[1]});
});

console.log(newResearch);