2

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.

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 .

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

my json is like this right now :

"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 ?

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

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;

});
5
  • 1
    do you need to transorm first JSON to second JSON? Commented Dec 14, 2017 at 9:33
  • @KresimirPendic 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 . Commented Dec 14, 2017 at 9:36
  • 1
    The structure you want to go to is not valid. It looks like you put the opening braces { in the wrong place. Commented Dec 14, 2017 at 9:45
  • I want to separate each part of array with room 1 and room 2 . @phuzi Commented Dec 14, 2017 at 9:49
  • @inaz I've updated your question to show valid structure Commented Dec 14, 2017 at 9:57

3 Answers 3

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:

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.

Sign up to request clarification or add additional context in comments.

5 Comments

this code does not work for me . and when I alert roomsMap it returns [object Object].
What is the exact output you require, since as written in the question, it's invalid syntax. Since I see you'd like something where "room1" : { "adultcount" : 0, "childcount" : 0 }, I thought you wanted an object with each room being a property, so you can use roomsMap[ 'room1' ] to access the first room. Alerting an object will always show that, try stingfiying the object or use the console to inspect it. So alert( JSON.stringify( roomsMap ) ); Or just plain old console.log( roomsMap ); in any debugging tool.
if returns always ObjectObject except console.log ,so how can I get the values? I want to get for example chidcount1 from room 1 and add it into a variable.
var childrenInRoom1 = roomsMap.room1.childcount; What you have here is basically transforming an array into a hashmap, so that you don't have to search through the array all the time to find the correct room. Since everythings an object now, you can just access it like any other object property.
0

You can access your json array using loop

$.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 Comments

I don't want to have acces to them I want to separate my array to each part that it have. for example room 1 , room 2 , room 3 ....
for this you can initialize a count variable outside of the loop and then concat that number with room so it will room1, room2...and then assign the value in that variable and if you want in an array then you can push in an array. don't forget to increase count in each iteration.
Rakesh, the count equals the index in the array + 1. No need for extra variables.
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 Comment

It returns [object Object].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.