javascript - 比较 2 个 JavaScript 对象并根据条件移动数据

标签 javascript arrays json object

我有一个名为 syncWithPreviousDay 的函数,它接收以下对象数组作为属性。

jsonObj

[
    {
        "Position": "1",
        "TrackName": "Rocket",
        "URL": "https://domain.local/nbs678"
    },
    {
        "Position": "2",
        "TrackName": "Dueling banjos",
        "URL": "https://domain.local/asd456"
    },
    {
        "Position": "3",
        "TrackName": "One hit wonder",
        "URL": "https://domain.local/xyz123"
    }
]

在这个函数中,我需要将 key URL 与数据库中具有相同 key 对的另一个对象数组进行比较,并且只要有匹配,就移动 key PositionCustomKeydata.chart 数组中的新键 PreviousPositionCustomKey。如果没有匹配项,请为两个键创建一个 null 值。

const syncWithPreviousDay = (jsonObj) => {

const data = {
    date: config.dateToday,
    chart: jsonObj
}

dbService.getDate(config.yesterday)
    .then( result => {
        console.log(result.chart)
    })
}

console.log 的结果如下所示:

[
    {
        "Position": "1",
        "TrackName": "One hit wonder",
        "URL": "https://domain.local/xyz123",
        "CustomKey": "x"
    },
    {
        "Position": "2",
        "TrackName": "Awesome old song",
        "URL": "https://domain.local/123qwe",
        "CustomKey": "y"
    },
    {
        "Position": "3",
        "TrackName": "Dueling banjos",
        "URL": "https://domain.local/asd456",
        "CustomKey": null
    }
]

所以我想要的data.chart应该看起来像这样:

[
    {
        "Position": "1",
        "TrackName": "Rocket",
        "URL": "https://domain.local/nbs678",
        "PreviousPosition": null,
        "CustomKey": null
    },
    {
        "Position": "2",
        "TrackName": "Dueling banjos",
        "URL": "https://domain.local/asd456",
        "PreviousPosition": "3",
        "CustomKey": null
    },
    {
        "Position": "3",
        "TrackName": "One hit wonder",
        "URL": "https://domain.local/xyz123",
        "PreviousPosition": "1",
        "CustomKey": "x"
    }
]

如何做到这一点?

最佳答案

您可以简单地通过迭代循环并匹配数据库数组中是否存在特定元素来完成此操作。如果值存在,则添加所需的值,否则添加空值。

我已经向您提供了逻辑,您可以在函数内或任何需要的地方使用它。

const localArr = [
    {
        "Position": "1",
        "TrackName": "Rocket",
        "URL": "https://domain.local/nbs678"
    },
    {
        "Position": "2",
        "TrackName": "Dueling banjos",
        "URL": "https://domain.local/asd456"
    },
    {
        "Position": "3",
        "TrackName": "One hit wonder",
        "URL": "https://domain.local/xyz123"
    }
];

const dbArray = [
    {
        "Position": "1",
        "TrackName": "One hit wonder",
        "URL": "https://domain.local/xyz123",
        "CustomKey": "x"
    },
    {
        "Position": "2",
        "TrackName": "Awesome old song",
        "URL": "https://domain.local/123qwe",
        "CustomKey": "y"
    },
    {
        "Position": "3",
        "TrackName": "Dueling banjos",
        "URL": "https://domain.local/asd456",
        "CustomKey": null
    }
];

for (const element of localArr) {
  const index = dbArray.findIndex(item => item.URL === element.URL);
  if (index !== -1) {
    element.PreviousPosition = dbArray[index].Position;
    element.CustomKey = dbArray[index].CustomKey;
  } else {
    element.PreviousPosition = null;
    element.CustomKey = null;
  }
}

console.log(localArr)

关于javascript - 比较 2 个 JavaScript 对象并根据条件移动数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60927225/

相关文章:

javascript - Meteor:如何在html页面上使用光标数组的数组显示列表

arrays - 如何就地洗牌

javascript - JSON — 使用 new Date(...) 解析错误

arrays - Swift:数组、文本字段和 UITableView

java - 从json字符串反序列化为对象时如何忽略null?

java - 从 pojo : how do I add "description" automatically? 生成 JsonSchema

javascript - 子菜单部分显示

javascript - Safari Jquery 事件处理程序延迟

javascript - 如何解决一个又一个的 promise ?

javascript - Node.js 的正确继承