Javascript 使用 jquery 从对象数组中获取属性值数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29472655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:24:29  来源:igfitidea点击:

Get array of property values from array of objects with jquery

javascriptjqueryarrays

提问by awimley

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 中是否有这样的快捷方式?

采纳答案by Alex Char

You can iterate through the object keys and save the namein the array using push:

您可以遍历对象键并name使用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()

Object.keys()

Array.prototype.forEach()

Array.prototype.forEach()

Array.prototype.push()

Array.prototype.push()

回答by m0meni

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 循环

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 可能有一个包含“更少代码”的快捷方式,但实际上它的性能会比你自己编写代码更差。

回答by Sherif Ahmed

You can use the $.mapfunction from jQuery :

您可以使用$.mapjQuery 中的函数:

var value = $.map(example, function () {
    return this.name;
});

This will return an array of the items.

这将返回一个项目数组。

回答by NYTom

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>

回答by Aleuck

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;
}