[英]combine two arrays in Javascript/react
i am trying to combine two arrays, i tried this:我想结合两个 arrays,我试过这个:
const admin_navigation = [...AdminNav, ...initialNav] - not working right its over writing one array const admin_navigation = [...AdminNav, ...initialNav] - 在写入一个数组时无法正常工作
i also tried, const admin_navigation = AdminNav.concat(initialNav);我也试过了, const admin_navigation = AdminNav.concat(initialNav); - give me an error (TypeError: AdminNav.concat is not a function) - 给我一个错误(TypeError:AdminNav.concat 不是函数)
what i am trying to create is one array that combine both arrays into one like the bottom example.我想要创建的是一个将 arrays 组合成一个的数组,就像底部的例子一样。
=== Array 1 === === 数组 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) === 数组 2 === (来自我的减速器)
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",
},
],
};
initialNav
and AdminNav
are objects, not arrays. initialNav
和AdminNav
是对象,而不是 arrays。 You want this instead:你想要这个:
const NewNav = { items: [...initialNav.items, ...AdminNav.items] };
Use spread operator or concat over the arrays:在 arrays 上使用扩展运算符或 concat:
const NewNav = {
items: [...AdminNav.items, ...initialNav.items]
};
You can try using concat
function also, which will return the result您也可以尝试使用concat
function ,这将返回结果
const items = AdminNav.items.concat(initialNav.items)
You can use the concat method which basically combines two existing arrays and creates a new one like this:您可以使用 concat 方法,它基本上结合了两个现有的 arrays 并创建一个新的,如下所示:
let firstNumbers = [1, 2, 3];
let secondNumbers = [4, 5, 6];
let merged = firstNumbers.concat(secondNumbers);
console.log(merged); // [1, 2, 3, 4, 5, 6]
You're trying to iterate the objects into an array.您正在尝试将对象迭代到数组中。
try尝试
const admin={...NewNav,...initialNav}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.