【发布时间】:2021-05-17 14:32:02
【问题描述】:
概述:
- 需要将值从一个对象交换到另一个对象的对象数组,反之亦然。
- 在本例中为“位置”键。
- 仅当不同键的不同值匹配时,它才会在对象之间执行交换。
- 在本例中为“ID”键。
我想我会发布这个问题/答案来帮助需要这种功能的人,因为我个人无法在网上找到它。
【问题讨论】:
标签: javascript arrays object array.prototype.map
概述:
我想我会发布这个问题/答案来帮助需要这种功能的人,因为我个人无法在网上找到它。
【问题讨论】:
标签: javascript arrays object array.prototype.map
功能:
//This will change the inputted object array no need for return
export function Object_array_swap_other_object_values_based_on_other_object_value(
object_array, //this is the object array that the swap is being performed on
change_key, //This is the key for which will have the changed value
Change_value_1, // this is what the object with the matching locating_value_1 will change to
Change_value_2, // this is what the object with the matching locating_value_2 will change to
locating_key, //what key do you want to match
locating_value_1, //what value do you want that locating key to be for the first change
locating_value_2 //what value do you want that locating key to be for the second change
) {
object_array.map(data => {
if (data[locating_key] == locating_value_1) {
data[change_key] = Change_value_1
} else if (data[locating_key] == locating_value_2) {
data[change_key] = Change_value_2
}
})
}
如何调用函数:
object_and_array_helpers.Object_array_swap_other_object_values_based_on_other_object_value(lists,
"position",
desitnation_postion,
origin_postion,
"id",
id_of_origin,
id_of_destination
)
这是我目前使用的。不过我敢肯定这可能会再次发生。
如果您有更好的方法来做到这一点。请给我看:)
【讨论】: