gpt4 book ai didi

javascript - 如何使用 jQuery 将值插入到特定索引处的二维数组中

转载 作者:行者123 更新时间:2023-11-28 08:13:47 25 4
gpt4 key购买 nike

我有以下 2 个数组:

groupedObjects: [
{ value: 125, currency: "EUR" },
{ value: 100, currency: "USD" },
{value: 320, currency: "RON" }
]
groupedObjects1: [
{ value: 500, currency: "EUR" },
{ value: 280, currency: "RON" }
]

如何让这两个数组看起来相同?我想要第二个数组,代码如下:

[
{ value: 500, currency: "EUR" },
{ value: 0, currency: "USD" },
{ value: 280, currency: "RON" }
]

我尝试过这段代码:

if ($(groupedObjects).length > $(groupedObjects1).length) {
_.each(groupedObjects,function(obj){
i++;
_.each(groupedObjects1,function(obj1){
if(obj.currency != obj1.currency) {
alert('Test');
groupedObjects1.push ({
value: '',
currency: ''
});
}
});
});
}

我希望两个数组都按货币排序,并且它们都应该具有相同数量的元素,无论哪个数组更大

最佳答案

    groupedObjects= [
{ value: 125, currency: "EUR" },
{ value: 100, currency: "USD" },
{value: 320, currency: "RON" }
];
groupedObjects1= [
{ value: 500, currency: "EUR" },
{ value: 280, currency: "RON" }
];


if ($(groupedObjects).length > $(groupedObjects1).length) {

var newArray = [];
$.each(groupedObjects, function (key, value) {
var flag = false;
var breakout = false;
flag = (function () {
$.each(groupedObjects1, function (key1, value1) {
if (value.currency === value1.currency) {
newArray.push(value1);
breakout = true;
return false;
}

});
if (!breakout) {
return true;
}
})();
if (flag) {
newArray.push({
value: 0,
currency: value.currency
});
}
});
groupedObjects1 = newArray;

}

关于javascript - 如何使用 jQuery 将值插入到特定索引处的二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23846233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com