【问题标题】:Replace each string in array with different object?用不同的对象替换数组中的每个字符串?
【发布时间】:2020-10-19 23:37:39
【问题描述】:

我有一个如下所示的数组:

[
    {
        "children": [
            "5efa29058a3e8a3efc45c11a",
            "5efa29158a3e8a3efc45c11b"
        ],
        "_id": "5efa29b88a3e8a3efc45c11f",
        "tabText": "foo",
        "headerText": "foobar",
        "__v": 0
    },
    {
        "children": [
            "5efa29228a3e8a3efc45c11c",
            "5efa292c8a3e8a3efc45c11d",
            "5efa29338a3e8a3efc45c11e"
        ],
        "_id": "5efa29ea8a3e8a3efc45c120",
        "tabText": "Foo2",
        "headerText": "foobar2",
        "__v": 0
    }
]

对于每个对象上的每个“children”数组,我需要获取孩子的 id,在数据库中查找它,然后将 id 字符串替换为包含数据库信息的对象。

猫鼬模型:

模型1:

const { Schema, model } = require('mongoose')

const Tab = model('tab', new Schema({
  tabText: String,
  headerText: String,
  children: Array
}))

module.exports = Tab

模型2:

const { Schema, model } = require('mongoose')

const Child = model('child', new Schema({
  icon: String,
  name: String,
  description: String
}))

module.exports = Child

【问题讨论】:

  • 到目前为止你尝试过什么?
  • 为什么不尝试使用一个 for 循环或一个 forEach?
  • 看起来您正在使用 MongoDB。您可以使用后端中的实际文档填充这些 id。您无需遍历文档,然后发送多个请求来填充每个文档。
  • @RameshReddy 我该怎么做?我知道如何在 SQL 中加入表,但在 mongo 中不知道,我不知道这是可能的。
  • @LoganB 你在用猫鼬吗?

标签: javascript node.js arrays json object


【解决方案1】:

应该像这样定义模型以形成关系。

模型1:

const { Schema, model } = require('mongoose')

const Tab = model('Tab', new Schema({
  tabText: String,
  headerText: String,
  children: [{ type: Schema.Types.ObjectId, ref: 'Child' }],
}))

module.exports = Tab

模型2:

const { Schema, model } = require('mongoose')
const Child = model('Child', new Schema({
  icon: String,
  name: String,
  description: String
}))
module.exports = Child

现在children 将引用Child 文档的ID。

当你想填充它们时,你可以使用:

await Tab.findOne().populate('children')

【讨论】:

  • 非常感谢您,这非常有效!我今天学到了一些新的东西,这对未来会有很大的帮助。感谢您的帮助!
【解决方案2】:

为了替换数组中的每个字符串。这是执行此操作的标准方法。

'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map'

const array1 = [1, 4, 9, 16];

// 传递一个函数给map

const map1 = array1.map(x => x * 2);

console.log(map1);

// 预期输出:数组 [2, 8, 18, 32]

【讨论】:

    【解决方案3】:

    这也是Promise.all 的一个好案例,因此您可以并行运行查询

    
    await Promise.all(
      input.map(async (item) => {
        return {
          ...item,
          children: await Promise.all(
            item.children.map(async (childId) => {
              return await getItemFromDatabase(childId);
            })
          ),
        };
      })
    );
    
    

    【讨论】:

      【解决方案4】:

      您只需要在数组上映射两次:

      // First: map over each element in the array.
      const newArray = array.map(arrElement => {
          // Now arrElement is just one of our array elements.
      
          // Then we can map over each element of the arrElement.children
          // to get each id separately.
          return arrElement.children.map(id => {
              // Do stuff with the id...
              // Make sure to return your new object from this function.
          })
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-14
        • 1970-01-01
        • 2011-04-13
        • 1970-01-01
        • 1970-01-01
        • 2016-03-09
        • 1970-01-01
        • 2022-12-15
        相关资源
        最近更新 更多