0

This is what I have:

<input type="checkbox" name="selectedId[{{ order.id }}]">

const orders = [];
$("input[type=checkbox][name*='selectedId']:checked").each(function (index) {
    let id          = $(this).attr('name').match(/[-0-9]+/);
    orders[index]   = id[0];
});

And I get:

orders = [0 => 1, 1 => 2]

What I would like to get is:

orders = [
    0 => ["id" => 1],
    1 => ["id" => 2]
];

orders[index]["id"] = id[0] doesn't work

1
  • 1
    orders[index] = { id: id[0] }; Commented Oct 7, 2020 at 8:40

1 Answer 1

2

Javascript has objects, not associative arrays. For your desired result, you'd want something like this:

let orders = [];
$("input[type=checkbox][name*='selectedId']:checked").each(function (index) {
    orders[index] = { id: $(this).attr('name').match(/[-0-9]+/)[0] };
});

or you could also use Array.push()

orders.push({ id: $(this).attr('name').match(/[-0-9]+/)[0] });

Also, const is meant for non-mutable constants, so you may want to use let instead.

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

Comments

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.