0

i am trying to combine two arrays, i tried this:
const admin_navigation = [...AdminNav, ...initialNav] - not working right its over writing one array
i also tried, const admin_navigation = AdminNav.concat(initialNav); - give me an error (TypeError: AdminNav.concat is not a function)

what i am trying to create is one array that combine both arrays into one like the bottom example.

=== Array 1 ===

const initialNav = {
  items: [
    {
      name: "Test Page",
      url: "/test/testpage",
      icon: "icon-drop",
    },
    {
      name: "Test Page2",
      url: "/test/testpage2",
      icon: "icon-drop",
    },
  ],
};

=== Array 2 === (comes from my reducer)

const AdminNav = {
  items: [
    {
      title: true,
      name: "ADMINISTRATOR22",
      wrapper: {
        // optional wrapper object
        element: "", // required valid HTML5 element tag
        attributes: {}, // optional valid JS object with JS API naming ex: { className: "my-class", style: { fontFamily: "Verdana" }, id: "my-id"}
      },
      class: "", // optional class names space delimited list for title item ex: "text-center"
    },
    {
      name: "Forms",
      url: "/base/forms",
      icon: "icon-puzzle",
    },
  ],
  //loading: false,
};

i need the results to be like this:

const NewNav = {
  items: [
    {
      title: true,
      name: "ADMINISTRATOR22",
      wrapper: {
        // optional wrapper object
        element: "", // required valid HTML5 element tag
        attributes: {}, // optional valid JS object with JS API naming ex: { className: "my-class", style: { fontFamily: "Verdana" }, id: "my-id"}
      },
      class: "", // optional class names space delimited list for title item ex: "text-center"
    },
    {
      name: "Forms",
      url: "/base/forms",
      icon: "icon-puzzle",
    },
    {
      name: "Test Page",
      url: "/test/testpage",
      icon: "icon-drop",
    },
    {
      name: "Test Page2",
      url: "/test/testpage2",
      icon: "icon-drop",
    },
  ],
};
3
  • Look into concat Commented May 9, 2020 at 17:14
  • I think spread operator resolves. [...arr1, ...arr2] Commented May 9, 2020 at 17:18
  • i tried....const admin_navigation = [...AdminNav, ...initialNav] - not working right its over writing one array i also tried, const admin_navigation = AdminNav.concat(initialNav); - give me an error (TypeError: AdminNav.concat is not a function) Commented May 9, 2020 at 17:19

5 Answers 5

3

initialNav and AdminNav are objects, not arrays. You want this instead:

const NewNav = { items: [...initialNav.items, ...AdminNav.items] };
Sign up to request clarification or add additional context in comments.

Comments

1

Use spread operator or concat over the arrays:

const NewNav = {
    items: [...AdminNav.items, ...initialNav.items]
};

Comments

0

You can use the concat method which basically combines two existing arrays and creates a new one like this:

let firstNumbers = [1, 2, 3];
let secondNumbers = [4, 5, 6];
let merged = firstNumbers.concat(secondNumbers);
console.log(merged); // [1, 2, 3, 4, 5, 6]

Comments

0

You're trying to iterate the objects into an array.

try

const admin={...NewNav,...initialNav}

Comments

0

You can try using concat function also, which will return the result

const items  = AdminNav.items.concat(initialNav.items)

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.