V
V
Vadim2015-10-18 19:34:53
JavaScript
Vadim, 2015-10-18 19:34:53

How to loop through json array using jQuery?

Greetings, friends! I need your help! There is a PHP array obtained from the database and the X-editable jQuery plugin:

$('.options').editable({
        value: [2, 3],
        source: [
            {value: 1, text: 'option1'},
            {value: 2, text: 'option2'},
            {value: 3, text: 'option3'}
        ]
    });


The result of its work is as follows:

847ba51982fd4d56b8e22d88f60d85cd.png

It is necessary to transfer the entire array of such a plan there:

$m = Array (
    [20] => Array
        (
            [value] => b12
            [text] => Сушить исключительно в тени
        )

    [21] => Array
        (
            [value] => c1
            [text] => Разрешена химчистка
        )

    [22] => Array
        (
            [value] => c2
            [text] => Химчистка запрещена
        )

    [23] => Array
        (
            [value] => c3
            [text] => Разрешено отбеливание
        )
);


But the thing is that in this form he does not perceive it, because of the first key. For example, this is what should happen:

foreach ($careInfo as $item) {
    $care['value'] = $item['care_key'];
    $care['text'] = $item['care_desc'];
}


$('.options').editable({
        value: [2, 3],
        source: [<?php echo json_encode($care);?>]
    });


And naturally received only one element of a multidimensional array:

Array
(
    [value] => d6
    [text] => Не отпаривать
)


Therefore, it turned out:

1de14034f07f4b109e29a0c05f868e62.png

Is there a way to iterate over an array like foreach, only with jQuery, to pass all the elements there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Nesmeyanov, 2015-10-18
@J01

I don't know about jquery, but:

for (var key in array) {
  var value = array[key];
  // console.log('key: ', key, 'value: ', value);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question