【问题标题】:Javascipt add parent to arrayJavascript将父级添加到数组
【发布时间】:2019-12-11 20:31:20
【问题描述】:

在 react-native-sectioned-multi-select 中显示一个数组时遇到问题,该数组接受有孩子的数组,我想创建这样的数组,

StaffData: [ {
name: 'Users',
id: 0,
// these are the children or 'sub items'
children: [
  {
    name: 'Apple',
    id: 10,
  },
  {
    name: 'Strawberry',
    id: 17,
  },
  {
    name: 'kolo',
    id: 13,
  },
  {
    name: 'mo',
    id: 14,
  },
  {
    name: 'n',
    id: 15,
  },
  {
    name: 'n',
    id: 16,
  },
],
}]

从此 基本上我希望下面的数组有一个像上面数组一样有'id'和'name'的父级

var secondarray = 
{"StaffData":
[{"id":1,"name":"Obi"},
{"id":2,"name":"Logistics"},
{"id":3,"name":"Security"},
{"id":4,"name":"Admin"},
{"id":5,"name":"Admin2"}]}

如果我做不到,如何在 react-native-sectioned-multi-select 上显示第二个数组

【问题讨论】:

  • 您能分享一下您一直在使用的代码吗?

标签: javascript arrays reactjs react-native


【解决方案1】:

我可能误解了这个问题,但由于secondarray.StaffData 似乎已经是您在StaffData.children 中想要的确切数据,只需使用您想要的格式的数组/对象文字:

let StaffData = [{
    name: 'Users',
    id: 0,
    children: secondarray.StaffData
}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 2015-08-07
    • 1970-01-01
    • 2016-07-22
    相关资源
    最近更新 更多