如何循环遍历 Object.values 数组?

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

How to loop Object.values array?

问题

如何从object.values数组中获取单个值?

我在console.log()中得到了这个对象 // {"item": Array(3)}

{"item": (3) ['21', '14', '12']}

我想从object.values中获取单个数字 // 21 ,14, 12

我尝试这样做

let j = Object.values(data)
  console.log(j)  // [Array(3)]
英文:

How to get single values from object.values array?

I am getting this obj in console.log() // {"item": Array(3)}

 {"item": (3) ['21', '14', '12']}

and I want to get single number from object.values // 21 ,14, 12

I am trying this

let j = Object.values(data)
  console.log(j)  // [Array(3)]

答案1

得分: 0

以下是翻译好的部分:

由于我不确定变量`data`中包含什么数据让我们假设`j`是一个具有项`items`的对象该项是一个数组

let j = {"items" : ['21', '14', '12']};

如果您想逐个打印数组中的每个元素您可以使用`forEach`

j.items.forEach(function(element) {
  console.log(element);
});

这将打印

21
14
12

或者如果您需要在一行中打印所有元素而不使用循环您可以使用`join`

let result = j.items.join(",");
console.log(result);

这将打印

21,14,12

您可以将`,`替换为您希望使用的任何分隔符
英文:

Since I am not sure what data is present in the variable data. Let's assume j is an object with item items which is an array.

let j = {"items" : ['21', '14', '12']};

If you want to print each element in the array individually, you can use forEach

j.items.forEach(function(element) {
  console.log(element);
});

This will print

21
14
12

or if you need to print all the elements in a single line without a loop, You can use join

let result = j.items.join(",");
console.log(result);

which will print

21,14,12

You can replace the , with whatever separator you wish to use.

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

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

let j = {&quot;items&quot; : [&#39;21&#39;, &#39;14&#39;, &#39;12&#39;]};

console.log(&quot;List with loop&quot;);
j.items.forEach(function(element) {
  console.log(element);
});

console.log(&quot;List with join&quot;);
let result = j.items.join(&quot;,&quot;);
console.log(result);

<!-- end snippet -->

答案2

得分: 0

Object.values() 函数返回对象中的所有值(不同属性的值) -- 以数组形式返回。

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_objects/Object/values

let data = {"item":["21", "14", "12"]}
let j = Object.values(data); // 以数组形式返回对象的值
j.map((item) => {
if(!Array.isArray(item)){
  console.log(item)
}
else{
item.map(val => console.log(val));
}
});
英文:

Object.values() function returns all values (of various properties) in an object -- itself in an array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values

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

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

let data = {&quot;item&quot;:[&#39;21&#39;, &#39;14&#39;, &#39;12&#39;]}
let j = Object.values(data); //returns object values in an array
j.map((item) =&gt; {
if(!Array.isArray(item)){
  console.log(item)
}
else{
item.map(val =&gt; console.log(val));
}
});

<!-- end snippet -->

答案3

得分: 0

在我的观点中,mapforEach更好。

let j = {"items": ['21', '14', '12']};
j.items.map((num) => console.log(num));

它将在控制台中打印输出:

21

14

12

英文:

in my option map is better than forEach

let j = {&quot;items&quot; : [&#39;21&#39;, &#39;14&#39;, &#39;12&#39;]};
j.items.map((num)=&gt;console.log(num));

it will print output in console

21

14

12

huangapple
  • 本文由 发表于 2023年6月2日 12:12:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387072.html
匿名

发表评论

匿名网友

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

确定