如何使用JavaScript循环创建嵌套对象数组?

huangapple go评论75阅读模式
英文:

How to created a nested array of objects using javascript Loop?

问题

我需要循环处理一个AJAX响应,然后创建一个像这样的数组:

"2023-03-30": {"number": '', "url": ""},
"2023-03-30": {"number": '', "url": ""}

循环代码如下:

var columns = {};

for (var i=0; i < parsed_data.tasks.length; i++) {
  var mydate = parsed_data.tasks[i].date; 
  columns.push(mydate);
}

console.log(columns);

但这不会给我所需的结果。

我该如何实现这个?

英文:

I need to loop an AJAX response and then create an array like this:

&quot;2023-03-30&quot;: {&quot;number&quot;: &#39;&#39;, &quot;url&quot;: &quot;&quot;},
&quot;2023-03-30&quot;: {&quot;number&quot;: &#39;&#39;, &quot;url&quot;: &quot;&quot;}

The loop looks like this:

var columns = {};

for (var i=0; i &lt; parsed_data.tasks.length; i++) {
  var mydate = parsed_data.tasks[i].date; 
  columns.push(mydate);
}

console.log(columns);

But this doesn't give me the required result.

How do I achieve this?

答案1

得分: 2

以下是翻译好的代码部分:

// 声明一个空对象来存储结果
var result = {};

// 遍历 AJAX 响应数组
for (i = 0; i < parsed_data.tasks.length; i++) {

  // 从每个对象中提取日期、编号和 URL 值
  var mydate = parsed_data.tasks[i].date;
  var number = parsed_data.tasks[i].number;
  var url = parsed_data.tasks[i].url;

  // 检查结果对象中是否已存在具有该日期的对象
  if (!result[mydate]) {

    // 如果不存在,创建一个新对象,以日期作为键和一个空对象作为值
    result[mydate] = {};

  }

  // 设置结果对象中相应对象的编号和 URL 值
  result[mydate]["number"] = number;
  result[mydate]["url"] = url;
}

// 输出最终结果
console.log(result);
英文:

You said you aren't getting the required response. What results are you actually getting? Maybe try something like this.

// Declare an empty object to store the result
var result = {};

// Loop through the AJAX response array
for (i = 0; i &lt; parsed_data.tasks.length; i++) {

  // Extract the date, number, and url values from each object
  var mydate = parsed_data.tasks[i].date;
  var number = parsed_data.tasks[i].number;
  var url = parsed_data.tasks[i].url;

  // Check if an object with the date already exists in the result object
  if (!result[mydate]) {

    // If it doesn&#39;t exist, create a new object with the date as the key and an empty object as the value
    result[mydate] = {};

  }

  // Set the number and url values for the corresponding object in the result object
  result[mydate][&quot;number&quot;] = number;
  result[mydate][&quot;url&quot;] = url;
}

// Output the final result
console.log(result);

答案2

得分: 0

If you need date you can use Object.keys like:

const array = [
  {"2023-03-30": {"number": '', "url": ''}},
  {"2023-03-30": {"number": '', "url": ''}}
];
const result = array.map(el => Object.keys(el)).flat();
console.log(result);
英文:

If you need date you can use Object.keys like:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const array = [
  {&quot;2023-03-30&quot;: {&quot;number&quot;: &#39;&#39;, &quot;url&quot;: &quot;&quot;}},
  {&quot;2023-03-30&quot;: {&quot;number&quot;: &#39;&#39;, &quot;url&quot;: &quot;&quot;}}
];
const result = array.map(el =&gt; Object.keys(el)).flat();
console.log(result);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 18:22:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定