-2

I want to add to existing array

var value_list = [
    ["asdf:4sdf", "", "", "", "asf:sadf", "", "", "", "sadf:2000/01/3", "", "", "", ""],
    ["safd", "asfd", "sdaf", "sadf", "asdf", "asdf", "sdf", "asfd", "sadf", "dsf", "sdf", "adf", "sadf"],
    ["1", "asdf", "asdf", "22", "22", "3000", "500", "0", "0", "3500", "0", "3500", ""],
    ["2", "asdf", "asdf", "22", "22", "3000", "500", "0", "0", "3500", "0", "3500", ""],
];

new array:

["SELECTIONS: ", "", str, "", "", "", "", "", "", "", "", "", ""]

Final result:

var value_list = [
    ["SELECTIONS: ", "", str, "", "", "", "", "", "", "", "", "", ""],
    ["asdf:4sdf", "", "", "", "asf:sadf", "", "", "", "sadf:2000/01/3", "", "", "", ""],
    ["safd", "asfd", "sdaf", "sadf", "asdf", "asdf", "sdf", "asfd", "sadf", "dsf", "sdf", "adf", "sadf"],
    ["1", "asdf", "asdf", "22", "22", "3000", "500", "0", "0", "3500", "0", "3500", ""],
    ["2", "asdf", "asdf", "22", "22", "3000", "500", "0", "0", "3500", "0", "3500", ""],
];

Tried var value_list = [new_array,value_list] but no success

Olian04
  • 6,480
  • 2
  • 27
  • 54
Nick Pietrushka
  • 125
  • 2
  • 11
  • 2
    Does this answer your question? [Push multiple elements to array](https://stackoverflow.com/questions/14723848/push-multiple-elements-to-array) – PEPEGA Apr 22 '21 at 14:35
  • 1
    Does this answer your question? [How can I add new array elements at the beginning of an array in Javascript?](https://stackoverflow.com/questions/8073673/how-can-i-add-new-array-elements-at-the-beginning-of-an-array-in-javascript) – Ivar Apr 22 '21 at 14:37
  • You just need to do `[newArray, ...value_list]` – DecPK Apr 22 '21 at 14:41

5 Answers5

1

this is probably what you want to do:

OPTION 1 – My personal choice

var result = [new_array, ...value_list]

OPTION 2

value_list.unshift(new_array)
  • Tried let value_selections = ["SELECTIONS: ", "", str, "", "", "", "", "", "", "", "", "", ""]; var value_list = value_list.unshift(value_selections); and gives me number? did i miss something ? – Nick Pietrushka Apr 22 '21 at 14:45
  • @NickPietrushka `unshift()` alters the array _in-place_ and returns the number of elements in the modified array. So don't use the return value, use the array itself. See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift). – Ivar Apr 22 '21 at 14:48
  • @Ivar could you please provide an example, used console.log(value_list.unshift(value_selections)); still number, one thing i need to put this array to variable – Nick Pietrushka Apr 22 '21 at 14:51
  • @NickPietrushka I edited my comment and added a link. By console logging you _still_ use the return value of `.unshift()`. Put the `.unshift()` on a separate line and then only log `value_list`. – Ivar Apr 22 '21 at 14:52
0

JavaScript Array method unshift will help you.

value_list.unshift(new_array)

Read about it here - https://www.w3schools.com/jsref/jsref_unshift.asp

PageSource
  • 301
  • 3
  • 11
-1

I think value_list.push(["SELECTIONS: ", "", str, "", "", "", "", "", "", "", "", "", ""]) should just work fine.

Oscar Umhin
  • 44
  • 1
  • 3
-1

This is the quick modern way:

let a = [1,2,3];
let b = [4,5,6];
let c = [...a,...b];
console.log(c);
Gal Gur-Arie
  • 376
  • 3
  • 6
-1

You just need to do

[new_array, ...value_list]

instead

[new_array,value_list]

var value_list = [
  [
    "asdf:4sdf",
    "",
    "",
    "",
    "asf:sadf",
    "",
    "",
    "",
    "sadf:2000/01/3",
    "",
    "",
    "",
    "",
  ],
  [
    "safd",
    "asfd",
    "sdaf",
    "sadf",
    "asdf",
    "asdf",
    "sdf",
    "asfd",
    "sadf",
    "dsf",
    "sdf",
    "adf",
    "sadf",
  ],
  [
    "1",
    "asdf",
    "asdf",
    "22",
    "22",
    "3000",
    "500",
    "0",
    "0",
    "3500",
    "0",
    "3500",
    "",
  ],
  [
    "2",
    "asdf",
    "asdf",
    "22",
    "22",
    "3000",
    "500",
    "0",
    "0",
    "3500",
    "0",
    "3500",
    "",
  ],
];

const new_array = [
  "SELECTIONS: ",
  "",
  "str",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
];

value_list = [new_array, ...value_list];
console.log(value_list);
DecPK
  • 24,537
  • 6
  • 26
  • 42