【问题标题】:How to insert multidimensional array data in react-native json?如何在 react-native json 中插入多维数组数据?
【发布时间】:2021-06-28 19:59:51
【问题描述】:

在这里,我包含了我的示例代码。我想将数据插入(推送)到我的多维 json。

  const answers = [
    { id: "1", text: "192.168.1.1" },
    { id: "2", text: "127.0.0.1", correct: true },
    { id: "3", text: "209.85.231.104" },
    { id: "4", text: "66.220.149.25" },
  ];

我想将 { id: "5", text: "66.220.149.20" }, 之类的元素插入到最后一个位置。 最终结果应该是这样的

  const answers = [
    { id: "1", text: "192.168.1.1" },
    { id: "2", text: "127.0.0.1", correct: true },
    { id: "3", text: "209.85.231.104" },
    { id: "4", text: "66.220.149.25" },
    { id: "5", text: "66.220.149.20" }
  ];

如何通过 react-native 实现多维 json 数据?或者可以这样做吗?

【问题讨论】:

标签: javascript json react-native


【解决方案1】:
  1. 我不明白这是多维的。通常,多维这个词与数组有关,看起来像这样。

    [[1, 2, 3], [4, 5, 6]]

  2. 我认为您正在寻找的是一种在 JSON 中找到最高 id 值并添加一个新对象(最高 id 值 + 1)的方法。

您的代码应如下所示。


let highestVal = answers.reduce(
    (highest, {id}) => highest > parseInt(id) ? highest : parseInt(id),
    0
)

answers.push({id: highestVal, text: '...', ...})

【讨论】:

    猜你喜欢
    • 2021-05-30
    • 1970-01-01
    • 2016-03-05
    • 2015-06-08
    • 2017-10-04
    • 2019-02-21
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多