单独的密钥和值对的两个数组 [英] Separate key and value pairs into two arrays

查看:73
本文介绍了单独的密钥和值对的两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是着手分离键和值分为两个不同阵列的最好方式,使这一点 -

  VAR数据= {一键:34,另一个关键:16,最后的钥匙:10};

将成为本 -

 数据1 = [一键,另一个关键,最后的关键];
DATA2 = [34,16,10];

感谢。


解决方案

  VAR数据= {一键:34,另一个关键:16,最后的钥匙:10 };VAR数据1 = []
    DATA2 = [];对于(数据VAR属性){   如果(!data.hasOwnProperty(财产)){
      继续;
   }   data1.push(财产);
   data2.push(数据[属性]);}


  1. 设置两个不同的空白阵列。

  2. 通过对象的枚举的属性迭代。

  3. 如果数据没有显式将此属性(即不上涨原型链),跳过此迭代。

  4. 按下键,将其值改为各自的数组。

的jsfiddle

What would be the best way to go about separating the key and values into two different arrays so that this -

var data = {"A Key": 34, "Another Key": 16, "Last Key": 10};

Would become this -

data1 = ["A Key", "Another Key", "Last Key"];
data2 = [34, 16, 10];

Thanks.

解决方案

var data = {"A Key": 34, "Another Key": 16, "Last Key": 10};

var data1 = [],
    data2 = [];

for (var property in data) {

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

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

}

  1. Set up two different blank arrays.
  2. Iterate through the enumerable properties of the object.
  3. If data does not have this property explicitly (i.e. not higher up the prototype chain), skip this iteration.
  4. Push the key and its value to their respective arrays.

jsFiddle.

这篇关于单独的密钥和值对的两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆