[英]How do I create a new array with a javascript for loop. I got the error "circular object Object"
I need to create a new array by dividing the returned array with ajax.我需要通过用ajax划分返回的数组来创建一个新数组。 I will copy the objects with offerCurrencyTypeExchanges value as much as the number of this array and write the code and rate value to the "label" value.我将复制具有与此数组数量一样多的 offerCurrencyTypeExchanges 值的对象,并将代码和费率值写入“标签”值。 But I think I created an error in the loop.但我想我在循环中创建了一个错误。 copying the last value to all.将最后一个值复制到所有。
Result looks better in jsfiddle https://jsfiddle.net/xd68no54/结果在 jsfiddle https://jsfiddle.net/xd68no54/ 中看起来更好
var res = [{
id: 3,
name: "Türk Lirası",
code: "TRY",
offerCurrencyTypeExchanges: [],
label: "Türk Lirası (TRY)"
},
{
id: 4,
name: "USD",
code: "USD",
offerCurrencyTypeExchanges: [{
exchangeRateId: 33642,
localize: "Alış",
value: 9.2857,
valueSection: 1
},
{
exchangeRateId: 33642,
localize: "Satış",
value: 9.3024,
valueSection: 2
},
{
exchangeRateId: 33642,
localize: "Efektif alış",
value: 9.2792,
valueSection: 3
},
{
exchangeRateId: 33642,
localize: "Efektif satış",
value: 9.3164,
valueSection: 4
}
]
},
{
id: 5,
name: "EUR",
code: "EUR",
offerCurrencyTypeExchanges: [{
exchangeRateId: 33643,
localize: "Alış",
value: 10.8108,
valueSection: 1
},
{
exchangeRateId: 33643,
localize: "Satış",
value: 10.8303,
valueSection: 2
},
{
exchangeRateId: 33643,
localize: "Efektif alış",
value: 10.8033,
valueSection: 3
},
{
exchangeRateId: 33643,
localize: "Efektif satış",
value: 10.8465,
valueSection: 4
}
]
},
{
id: 6,
name: "TRY",
code: "TRY",
offerCurrencyTypeExchanges: [],
label: "TRY (TRY)"
}
]
var resultAutoComplete = [];
var arrInObj = {};
for (var i = 0; i < res.length; i++) {
if (res[i].offerCurrencyTypeExchanges.length == 0) {
arrInObj = {
code: res[i].code,
id: res[i].id,
name: res[i].name,
label: res[i].label = res[i].name + " " + "(" + res[i].code + ")"
}
resultAutoComplete.push(arrInObj)
} else {
arrInObj = {
code: res[i].code,
id: res[i].id,
name: res[i].name,
}
for (var j = 0; j < res[i].offerCurrencyTypeExchanges.length; j++) {
arrInObj.label = res[i].code + " - " + res[i].offerCurrencyTypeExchanges[j].localize + "(" + res[i].offerCurrencyTypeExchanges[j].value + ")";
arrInObj.exchangeRateId = res[i].offerCurrencyTypeExchanges[j].exchangeRateId
arrInObj.localize = res[i].offerCurrencyTypeExchanges[j].localize
arrInObj.value = res[i].offerCurrencyTypeExchanges[j].value
arrInObj.valueSection = res[i].offerCurrencyTypeExchanges[j].valueSection
resultAutoComplete.push(arrInObj)
}
}
console.log(resultAutoComplete)
}
Süleyman TAŞÇI, can you provide an example of what you'd like to see as your result? Süleyman TAŞÇI,您能否举例说明您希望看到的结果? I think that may clarify what you're asking.我认为这可能会澄清你在问什么。 I would be happy to try and help!我很乐意尝试并提供帮助!
我不是很明白你想做什么,但你可以提取你的对象并用它们做你想做的事情。
const currencyExchangeTypes = res.map((c) => c.offerCurrencyTypeExchanges).reduce((a, b) => a.concat(b), []);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.