This question already has an answer here:
这个问题在这里已有答案:
- From an array of objects, extract value of a property as array 12 answers
- 从一组对象中,提取属性的值作为数组12的答案
I am trying to get from here:
我想从这里开始:
example = [{
name: "someone1",
city: "somewhere1",
state: "someplace1"
},{
name: "someone2",
city: "somewhere2",
state: "someplace2"
}]
to here:
到这里:
example.name = [ "someone1", "someone2" ]
In as little a code as possible. Obviously I could just loop it and build the array but I need to do this a large number of times on a variety of objects. I could write a function to do it but it would be difficult to make the function general enough for my application.
尽可能少的代码。显然我可以循环它并构建数组,但我需要在各种对象上进行多次这样的操作。我可以编写一个函数来完成它但是很难使我的应用程序的函数足够通用。
Is there a shortcut for this in jQuery?
在jQuery中有这个快捷方式吗?
5 个解决方案
#1
7
You can iterate through the object keys and save the name
in the array using push
:
您可以使用push遍历对象键并将名称保存在数组中:
example = [{
name: "someone1",
city: "somewhere1",
state: "someplace1"
}, {
name: "someone2",
city: "somewhere2",
state: "someplace2"
}];
var arrNames = [];
//iterate through object keys
Object.keys(example).forEach(function(key) {
//get the value of name
var val = example[key]["name"];
//push the name string in the array
arrNames.push(val);
});
console.log(arrNames);//prints ["someone1", "someone2"]
After @Felix suggestion(with which I agree) there is no need to use Object.keys
:
在@Felix建议(我同意)之后,不需要使用Object.keys:
example = [{
name: "someone1",
city: "somewhere1",
state: "someplace1"
}, {
name: "someone2",
city: "somewhere2",
state: "someplace2"
}];
var arrNames = [];
//iterate through object keys
example.forEach(function(item) {
//get the value of name
var val = item.name
//push the name string in the array
arrNames.push(val);
});
console.log(arrNames); //prints ["someone1", "someone2"]
References
参考
Object.keys()
Array.prototype.forEach()
Array.prototype.push()
#2
5
I made a quick test for you here: http://jsperf.com/jquery-vs-javascriptlvjsklvjsfklsfklsdjfk
我在这里为你做了一个快速测试:http://jsperf.com/jquery-vs-javascriptlvjsklvjsfklsfklsdjfk
It consists of three solutions:
它由三个解决方案组成:
A basic for loop
一个基本的循环
for (var i = 0; i < example.length; i++) {
array.push(example[i].name);
}
A jQuery $.each()
一个jQuery $ .each()
$.each(example, function(i, item) {
array.push(example[i].name);
});
And one answer posted to this thread
一个答案贴在这个帖子上
Object.keys(example).forEach(function(key) {
//get the value of name
var val = example[key]["name"];
//push the name string in the array
array.push(val);
});
Here are the results (bigger bar is better)
结果如下(更大的条形更好)
Basically what you have to keep in mind is that jQuery might have a shortcut which consists of "less code", but in reality it'll have worse performance than if you wrote the code yourself.
基本上你必须要记住的是,jQuery可能有一个由“less code”组成的快捷方式,但实际上它的性能会比你自己编写代码的性能差。
#3
5
You can use the $.map
function from jQuery :
您可以使用jQuery中的$ .map函数:
var value = $.map(example, function () {
return this.name;
});
This will return an array of the items.
这将返回一个项目数组。
#4
2
Here is how to do it using jinqJs
以下是使用jinqJs的方法
The line of code is:
代码行是:
jinqJs().from(example).select(function(row){return row.name;});
var example = [{
name: "someone1",
city: "somewhere1",
state: "someplace1"
},{
name: "someone2",
city: "somewhere2",
state: "someplace2"
}];
var result = jinqJs().from(example).select(function(row){return row.name;});
document.body.innerHTML = '<pre>' + JSON.stringify(result, null, 2) + '</pre><br><br>';
<script src="https://rawgit.com/fordth/jinqJs/master/jinqjs.min.js"></script>
#5
1
This is the simplest solution I can think of:
这是我能想到的最简单的解决方案:
function doThat (myArray) {
newObject = {};
myArray.forEach(function (obj, idx) {
var keys = Object.keys(obj);
keys.forEach(function (key) {
if (!obj[key]) {
newObject[key] = [];
}
newObject[key][idx] = obj[key];
});
});
return newObject;
}
#1
7
You can iterate through the object keys and save the name
in the array using push
:
您可以使用push遍历对象键并将名称保存在数组中:
example = [{
name: "someone1",
city: "somewhere1",
state: "someplace1"
}, {
name: "someone2",
city: "somewhere2",
state: "someplace2"
}];
var arrNames = [];
//iterate through object keys
Object.keys(example).forEach(function(key) {
//get the value of name
var val = example[key]["name"];
//push the name string in the array
arrNames.push(val);
});
console.log(arrNames);//prints ["someone1", "someone2"]
After @Felix suggestion(with which I agree) there is no need to use Object.keys
:
在@Felix建议(我同意)之后,不需要使用Object.keys:
example = [{
name: "someone1",
city: "somewhere1",
state: "someplace1"
}, {
name: "someone2",
city: "somewhere2",
state: "someplace2"
}];
var arrNames = [];
//iterate through object keys
example.forEach(function(item) {
//get the value of name
var val = item.name
//push the name string in the array
arrNames.push(val);
});
console.log(arrNames); //prints ["someone1", "someone2"]
References
参考
Object.keys()
Array.prototype.forEach()
Array.prototype.push()
#2
5
I made a quick test for you here: http://jsperf.com/jquery-vs-javascriptlvjsklvjsfklsfklsdjfk
我在这里为你做了一个快速测试:http://jsperf.com/jquery-vs-javascriptlvjsklvjsfklsfklsdjfk
It consists of three solutions:
它由三个解决方案组成:
A basic for loop
一个基本的循环
for (var i = 0; i < example.length; i++) {
array.push(example[i].name);
}
A jQuery $.each()
一个jQuery $ .each()
$.each(example, function(i, item) {
array.push(example[i].name);
});
And one answer posted to this thread
一个答案贴在这个帖子上
Object.keys(example).forEach(function(key) {
//get the value of name
var val = example[key]["name"];
//push the name string in the array
array.push(val);
});
Here are the results (bigger bar is better)
结果如下(更大的条形更好)
Basically what you have to keep in mind is that jQuery might have a shortcut which consists of "less code", but in reality it'll have worse performance than if you wrote the code yourself.
基本上你必须要记住的是,jQuery可能有一个由“less code”组成的快捷方式,但实际上它的性能会比你自己编写代码的性能差。
#3
5
You can use the $.map
function from jQuery :
您可以使用jQuery中的$ .map函数:
var value = $.map(example, function () {
return this.name;
});
This will return an array of the items.
这将返回一个项目数组。
#4
2
Here is how to do it using jinqJs
以下是使用jinqJs的方法
The line of code is:
代码行是:
jinqJs().from(example).select(function(row){return row.name;});
var example = [{
name: "someone1",
city: "somewhere1",
state: "someplace1"
},{
name: "someone2",
city: "somewhere2",
state: "someplace2"
}];
var result = jinqJs().from(example).select(function(row){return row.name;});
document.body.innerHTML = '<pre>' + JSON.stringify(result, null, 2) + '</pre><br><br>';
<script src="https://rawgit.com/fordth/jinqJs/master/jinqjs.min.js"></script>
#5
1
This is the simplest solution I can think of:
这是我能想到的最简单的解决方案:
function doThat (myArray) {
newObject = {};
myArray.forEach(function (obj, idx) {
var keys = Object.keys(obj);
keys.forEach(function (key) {
if (!obj[key]) {
newObject[key] = [];
}
newObject[key][idx] = obj[key];
});
});
return newObject;
}