简体   繁体   English

如何使用 javascript for 循环创建新数组。 我收到错误“圆形对象对象”

[英]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.

相关问题 如何从Javascript中的现有对象创建新对象? - How do I create a new object from an existing object in Javascript? 在 JavaScript 中,如何创建一个包含其他对象数组的对象? - In JavaScript, How do I create an object that has an array of other object? 如何使用 json 数组创建一个新的 object,只有名称-值对和 javascript? - How do I use a json array to create a new object with only name-value pairs with javascript? 如何删除 JavaScript 中 object 的所有循环引用? - How do I remove all circular references from an object in JavaScript? 如何在JavaScript中将新对象添加到数组? - How do I add a new Object to a Array, in JavaScript? 如何遍历 JavaScript object 中的相似键值对(a0,a1,a2)并生成一个没有键中数字的新数组(a)? - How do I loop through similar key value pairs(a0,a1,a2) in JavaScript object and generate a new array without the number in the key(a)? 我如何使用javascript中具有相同键的对象从对象中创建具有对象数组的对象数组 - how do I create an array of objects with array in the object from an object with the same key in javascript 如何创建这样的JavaScript对象? - How do I create a javascript object like this? 如何在javascript中创建打字稿对象? - How do I create a typescript object in javascript? 如何在JavaScript中创建统一的对象? - How do I create an unitialized object in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM