最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to split a js object into an array of key-value pairs? - Stack Overflow

programmeradmin3浏览0评论

Let's say I have an object like this:

var myObject = {
   X:1,
   Y:2,
   Z:3
}

Let's say I want to create a for loop to process each property on the object. The code below is just intended to be some pseudocode to municate what I'm looking to do:

var properties = myObject.split();
for(var i = 0; i < properties.length; i++)
{
   var x = properties[i][key];
   var y = properties[i][value]
}

Can you remend some code I can use to acplish this in Javascript/TypeScript?

Let's say I have an object like this:

var myObject = {
   X:1,
   Y:2,
   Z:3
}

Let's say I want to create a for loop to process each property on the object. The code below is just intended to be some pseudocode to municate what I'm looking to do:

var properties = myObject.split();
for(var i = 0; i < properties.length; i++)
{
   var x = properties[i][key];
   var y = properties[i][value]
}

Can you remend some code I can use to acplish this in Javascript/TypeScript?

Share Improve this question asked Dec 20, 2017 at 4:22 user8570495user8570495 1,8356 gold badges23 silver badges35 bronze badges 1
  • 1 Object.keys(myObject).forEach(key => { console.log(key, myObject[key]) }) – Aaron Beall Commented Dec 20, 2017 at 4:34
Add a ment  | 

5 Answers 5

Reset to default 7

You could use the Object.entries function to get all the key and value pairs as an array of arrays:

Object.entries(myObject);

Which would return:

[['X', 1], ['Y', 2], ['Z' 3]]

And you could iterate through them:

for(const [key, value] of Object.entries(myObject)) {
  console.log(key, value);
}

Which would log out:

X 1
Y 2
Z 3

Do note that this is a relatively new feature with no support on IE nor Opera. You can use the polyfill from MDN or use any of the other methods (for…in with hasOwnProperty, Object.keys, etc.).

var arr = {
        X:1,
        Y:2,
        Z:3
    };


Object.keys(arr).map(key => { console.log(key, arr[key]) })

Here is the solution.

var arr = {
    X:1,
    Y:2,
    Z:3
};

for (var key in arr) {
    if (arr.hasOwnProperty(key)) {
        console.log(key + " -> " + arr[key]);
    }
}

Another way you can do is

var arr = {
        X:1,
        Y:2,
        Z:3
    };


Object.keys(arr).forEach(key => { console.log(key, arr[key]) })

Best practice is typically using Object.keys().

var keys = Object.keys(myObject);
for(var i = 0; i < keys; i++) {
  var key, value;
  key = Object.keys[i];
  value = myObject[key];

  // .. do something else ..
}

You can do this Object keys method. Create a custom function and add it to the prototype of the Object like,

var myObject = {
  X: 1,
  Y: 2,
  Z: 3
}

Object.prototype.mySplit = function() {
  return Object.keys(this).map((key) => {
    return {
      key: key,
      value: this[key]
    };
  });
}

var properties = myObject.mySplit();
for (var i = 0; i < properties.length; i++) {
  console.log(properties[i]['key'], properties[i]['value']);
}

发布评论

评论列表(0)

  1. 暂无评论