【问题标题】:add keys to a json file, remove duplicates and write to json file in javascript将键添加到 json 文件,删除重复项并在 javascript 中写入 json 文件
【发布时间】:2022-01-13 13:26:27
【问题描述】:

我想将另一个 Json 文件中的数据添加到另一个而不覆盖现有文件。我无法再进一步了,控制台总是给我以下信息:

Console output 
Data 
string  
[
    "follow1",
    "follow2",
    "follow3",
    "follow4",
    "[\"follow5\",\"follow6\",\"follow7\",\"follow8\",\"follow9\"]"
]

这是我的代码,我想添加数据但没有方括号和反斜杠。如果有人可以帮助我,那就太好了。非常感谢

const user = require('./user.json');
const oldUser = user.user_follwos["user1"];
const data = require('./data.json');
const toAdd = JSON.stringify(data);

const result = JSON.stringify(toAdd);
oldUser.push(...toAdd.split(' '))
const newData = JSON.stringify(oldUser, null, 4)
console.log('\nData \n' + typeof newData + '  \n' + newData);

这是我的 json 文件

//user.json
{
  "application_id": "123546789",
  "user_follwos": {
    "user1": [
      "follow1",
      "follow2",
      "follow3",
      "follow4"
    ],
    "user2": [
      "followA",
      "followB",
      "followC",
      "followD"
    ]
  },
 ...
 ...
}
//data.json
[
  "follow5",
  "follow6",
  "follow7",
  "follow8",
  "follow9"
]

【问题讨论】:

    标签: javascript node.js json writefile


    【解决方案1】:

    首先要做的事情你需要json中的两个数据

    • 制作 2 个数组
    • 删除重复项
    • 然后推送数据,不重复。

    把所有东西放在一起

    let allTogether = data.push(...oldUser);
    

    创建唯一数组

    uniq = [...new Set(allTogether )];
    

    最后将此唯一数据设置为特定键

    user_follwos.user1 = uniq
    

    希望这是你需要的

    更新示例

        let user = {
        "application_id": "123546789",
        "user_follwos": {
        "user1": [
          "follow1",
          "follow2",
          "follow3",
          "follow4"
        ],
        "user2": [
          "followA",
          "followB",
          "followC",
          "followD"
        ]
      }
    };
    
    let data = [
      "follow5",
      "follow6",
      "follow7",
      "follow8",
      "follow9"
    ];
    
    let oldUser = user["user_follwos"]["user1"];
    console.log(`This is old user array`);
    console.log(oldUser);
    let allTogether = [];
    allTogether.push(...data)
    allTogether.push(...oldUser);
    console.log(`After we put all together`);
    console.log(allTogether);
    uniq = [...new Set(allTogether )];
    console.log(`Getting unique values`);
    console.log(uniq);
    oldUser = uniq;
    console.log(`Now olds user is`);
    console.log(oldUser);
    

    【讨论】:

    • 您好,感谢您的解决方案。当我运行您的解决方案时,我收到此错误 TypeError: number 107 is not iterable (cannot read property Symbol(Symbol.iterator))
    • 看来这个方法是非常新的代码风格,我会回到电脑上用旧的方式写。随时欢迎您
    • @daf 现在请运行一个示例,我检查过它可以工作。非常感谢。
    【解决方案2】:

    您不需要对数据变量进行字符串化,因为它是一个 javascript 对象,因此您可以将其连接到现有用户数组的末尾。

    const user = require('./user.json');
    let oldUser = user.user_follwos["user1"];
    const data = require('./data.json');
    
    oldUser = oldUser.concat(data);
    const newData = JSON.stringify(oldUser, null, 4);
    console.log('\nData \n' + typeof newData + '  \n' + newData);
    

    concat 方法创建一个新的数组对象,因此您可以将结果分配给一个新变量,而不会覆盖您现有的“oldUser”变量。

    Data
    string
    [
        "follow1",
        "follow2",
        "follow3",
        "follow4",
        "follow5",
        "follow6",
        "follow7",
        "follow8",
        "follow9"
    ]
    

    【讨论】:

      【解决方案3】:

      在将结果写入文件之前,您应该在最后一次将您的数据结构准确地转换为 JSON。

      就目前而言,您正在利用每一个机会将所有内容转换为 JSON。因此,每次您尝试向数据结构中添加一个数组时,您都会添加一个表示该数组的 JSON 字符串。

      【讨论】:

        猜你喜欢
        • 2018-12-04
        • 2013-04-22
        • 1970-01-01
        • 2017-10-17
        • 2017-05-08
        • 2021-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多