-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

3

5 Answers 5

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Tried let value_selections = ["SELECTIONS: ", "", str, "", "", "", "", "", "", "", "", "", ""]; var value_list = value_list.unshift(value_selections); and gives me number? did i miss something ?
@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.
@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
@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.
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

Comments

-1

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

1 Comment

Hey – Push will add it to the end, I think his specific use case wants it at the start
-1

This is the quick modern way:

let a = [1,2,3];
let b = [4,5,6];
let c = [...a,...b];
console.log(c);

Comments

-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);

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.