简体   繁体   English

如何将具有键和值的对象分成两个数组

[英]How to separate an object with keys and values into two arrays

I have an object:我有一个对象:

var myObj = {
  name: 'dri',
  age: '22',
  sex: 'female'
}

I need separate the keys and the values in two arrays.我需要将两个数组中的键和值分开。

var keys = [];
var values = [];

How can I do that?我怎样才能做到这一点?

You can try this:-你可以试试这个:-

var keys = [];
var values = [];
for (var prop in data) {
   keys.push(prop);
   values.push(data[prop]);
}

Try this:尝试这个:

var data = {"name": "dri", "age": 22, "sex": "female"};

var data1 = [],
    data2 = [];

for (var property in data) {

   if ( ! data.hasOwnProperty(property)) {
      continue;
   }

   data1.push(property);
   data2.push(data[property]);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过将对象键与值匹配来映射两个单独的数组 - Map two separate arrays by matching object keys to values 如何从一个对象创建一个字符串数组,其中各个键的值是单独的字符串数组? - How do I create an array of strings from an object where the values of individual keys are separate arrays of strings? 如何将两个 arrays 作为键和值合并到 object 中,键具有多个值? - How to Merge the two arrays as keys and values into an object with keys having more than one value? 如何将两个数组组合成一个对象,使第一个数组的值为键,第二个数组的值为值 - How to combine two arrays into one object so that the values of the first array are keys, and the values of the second array are values 如何使用键将两个数组合并到一个对象中 - How to Combine two arrays into an object with keys 如何将键数组和值数组合并到一个对象中? - How to merge array of keys and arrays of values into an object? 如何插入 SQL 这个 object - keys + Arrays of values - How to insert into SQL this object - keys + Arrays of values 在按值排序的单独 arrays 中获取值和键 - getting values and keys in separate arrays sorted by values 使用下划线将两个键和值数组合并到一个对象中 - merge two arrays of keys and values to an object using underscore 将键和值与对象分开 - Separate keys and values from object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM