0

Here I have an array with object: (array can have N number of objects...)

[Object, Object]
  0: Object
    0: "2"
    1: "Laza Lazic"
    id_tabele: "2"
    naziv: "Laza Lazic"
__proto__: Object
 1: Object
    0: "1"
    1: "Pera Peric"
    id_tabele: "1"
    naziv: "Pera Peric"
__proto__: Object
length: 2
__proto__: Array[0]

How I can convert this array of object to this format with javascript (jquery):

[{"id":"1","ime_prezime":"Pera Peric"},{"id":"2","ime_prezime":"Laza Lazic"}]

so id_tabele from array object is id in json, and naziv is ime_prezime in json...

1
  • also '0' is id, '1' is ime_prezime ... Commented Mar 22, 2014 at 0:40

1 Answer 1

1

Use $.map()

var array = $.map(oldarray, function (obj) {
    return {
        id: obj.id_tabele,
        naziv: obj.ime_prezime
    };
})
Sign up to request clarification or add additional context in comments.

2 Comments

i dont get json in this format: [{"id":"1","ime_prezime":"Pera Peric"},{"id":"2","ime_prezime":"Laza Lazic"}]
with JSON.strigify i get this: [{"0":"2","1":"Laza Lazic","id_tabele":"2","naziv":"Laza Lazic"},{"0":"1","1":"Pera Peric","id_tabele":"1","naziv":"Pera Peric"}] but this is not what i need

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.