获取对象值而不使用循环[duplicate]

时间:2022-03-23 07:37:32

This question already has an answer here:

这个问题已经有了答案:

I have following object:

我有以下对象:

var input = {
  'foo': 2,
  'bar': 6,
  'baz': 4
};

Is it possible to get values from this object without looping it ?

是否可以从这个对象中获取值而不进行循环?

It is possible to use jQuery.

可以使用jQuery。

Expected result:

预期结果:

var output = [2, 6, 4];

5 个解决方案

#1


7  

var arr = $.map(input,function(v){
 return v;
});

Demo --> http://jsfiddle.net/CPM4M/

演示- - > http://jsfiddle.net/CPM4M/

#2


6  

This is simply not possible without a loop. There's no Object.values() method (yet) to complement Object.keys().

如果没有循环,这是不可能的。还没有Object.values()方法来补充Object.keys()。

Until then you're basically "stuck" with the below construct:

在此之前,你基本上是“被”以下结构所困:

var values = [];

for (var k in input) {
  if (input.hasOwnProperty(k)) {
    values.push(input[k]);
  }
}

Or, in modern browsers (but of course still using a loop and an anonymous function call):

或者,在现代浏览器中(当然仍然使用循环和匿名函数调用):

var values = Object.getOwnPropertyNames(input).map(function(key) {
    return input[key];
});

#3


3  

I don't know why you want without loop . here my solution

我不知道你为什么要没有循环。在这里我的解决方案

JSON.stringify( input ).replace(/"(.*?)"\:|\{|\}/g,'' ).split(',')

it print [2, 6, 4] . I didn't test for other json values

它打印[2,6,4]。我没有测试其他json值

#4


1  

You can get values from this object without looping using Object.values() method like:

可以使用objec .values()方法从该对象获取值,而不使用循环:

var output = Object.values( input );
console.log( output );  // [2, 6, 4]

DEMO:

演示:

var input = {
  'foo': 2,
  'bar': 6,
  'baz': 4
};

var output = Object.values( input );
console.log( output );

PLEASE NOTE:

请注意:

This is an experimental technology

这是一项实验技术

Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

由于该技术的规范尚未稳定,请检查兼容性表,以便在各种浏览器中使用。还要注意,随着规范的改变,实验技术的语法和行为也会随着浏览器未来版本的变化而变化。

So, currently it supports Chrome & Firefox only.

所以,目前它只支持Chrome和Firefox。

#5


-2  

var input = {
  'foo': 2,
  'bar': 6,
  'baz': 4
};
var newArr = new Array;
$.each(input,function(key,value) {
    newArr.push(value);
});
alert(newArr)

#1


7  

var arr = $.map(input,function(v){
 return v;
});

Demo --> http://jsfiddle.net/CPM4M/

演示- - > http://jsfiddle.net/CPM4M/

#2


6  

This is simply not possible without a loop. There's no Object.values() method (yet) to complement Object.keys().

如果没有循环,这是不可能的。还没有Object.values()方法来补充Object.keys()。

Until then you're basically "stuck" with the below construct:

在此之前,你基本上是“被”以下结构所困:

var values = [];

for (var k in input) {
  if (input.hasOwnProperty(k)) {
    values.push(input[k]);
  }
}

Or, in modern browsers (but of course still using a loop and an anonymous function call):

或者,在现代浏览器中(当然仍然使用循环和匿名函数调用):

var values = Object.getOwnPropertyNames(input).map(function(key) {
    return input[key];
});

#3


3  

I don't know why you want without loop . here my solution

我不知道你为什么要没有循环。在这里我的解决方案

JSON.stringify( input ).replace(/"(.*?)"\:|\{|\}/g,'' ).split(',')

it print [2, 6, 4] . I didn't test for other json values

它打印[2,6,4]。我没有测试其他json值

#4


1  

You can get values from this object without looping using Object.values() method like:

可以使用objec .values()方法从该对象获取值,而不使用循环:

var output = Object.values( input );
console.log( output );  // [2, 6, 4]

DEMO:

演示:

var input = {
  'foo': 2,
  'bar': 6,
  'baz': 4
};

var output = Object.values( input );
console.log( output );

PLEASE NOTE:

请注意:

This is an experimental technology

这是一项实验技术

Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

由于该技术的规范尚未稳定,请检查兼容性表,以便在各种浏览器中使用。还要注意,随着规范的改变,实验技术的语法和行为也会随着浏览器未来版本的变化而变化。

So, currently it supports Chrome & Firefox only.

所以,目前它只支持Chrome和Firefox。

#5


-2  

var input = {
  'foo': 2,
  'bar': 6,
  'baz': 4
};
var newArr = new Array;
$.each(input,function(key,value) {
    newArr.push(value);
});
alert(newArr)