【发布时间】:2022-01-24 05:31:44
【问题描述】:
我有两个数组 array1 和数组 2.Array1 的值的键名为“discount_price”,array2 的值的键名为“regular_price”。 例如: discount_price_array, regular_price_array
当我使用数组合并合并它们时,我得到一个包含组合值的数组。即如果 array1 有 10 个元素,而 array2 有 10 个元素,它会合并成一个有 20 个元素的数组。 merged_array.
我想要的是一个数组,例如:
array{"discount_price":10,"regular_price":2},
array{"discount_price":4,"regular_price":3},
我怎样才能做到这一点?
$(values).each(function(key, value) {
//console.log(value);
if( value.discount_price !==undefined){
discount_price_array.push({ discount_price: value.discount_price })
}
});
$(values).each(function(key, value) {
//console.log(value);
if( value.regular_price !==undefined){
regular_price_array.push({ regular_price: value.regular_price })
}
});
var finalarray =$.merge(discount_price_array,regular_price_array
)
【问题讨论】:
-
两个数组的长度一样吗?
-
@SaeedShamloo 是的
-
可以使用纯js的
map或者$.map来代替merge。 -
欢迎来到 Stack Overflow。你看过api.jquery.com/jquery.extend吗?
标签: javascript jquery