如何解决在其他键值匹配的对象数组中交换值
概述:
- 需要将值从一个对象交换到另一个对象的对象数组,反之亦然。
- 在本例中为“位置”键。
- 它只会在匹配不同键的不同值时执行对象之间的交换。
- 在本例中为“ID”键。
我想我会发布这个问题/答案来帮助需要这种功能的人,因为我个人在网上找不到它。
解决方法
功能:
//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
)
这是我目前使用的。不过,我确信这可以再次骨折。
如果你有更好的方法来做到这一点。请告诉我:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。