英文:
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 = {"items" : ['21', '14', '12']};
console.log("List with loop");
j.items.forEach(function(element) {
console.log(element);
});
console.log("List with join");
let result = j.items.join(",");
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 = {"item":['21', '14', '12']}
let j = Object.values(data); //returns object values in an array
j.map((item) => {
if(!Array.isArray(item)){
console.log(item)
}
else{
item.map(val => console.log(val));
}
});
<!-- end snippet -->
答案3
得分: 0
在我的观点中,map比forEach更好。
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 = {"items" : ['21', '14', '12']};
j.items.map((num)=>console.log(num));
it will print output in console
21
14
12
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论