将对象转换为对象数组

将对象转换为对象数组

本文介绍了将对象转换为对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从WCF调用中返回了以下javascript对象,该对象已从字典对象中序列化,该字典对象删除了键/值属性

I have the following javascript object being returned from a WCF call, this has been serialized from a dictionary object, which removed the Key/Value properties

Object { 7="XXX", 9="YYY" }

我想将此JavaScript转换为以下数组,结果为

I want to convert this javascript in to the following array, with result being

[Object { Key=7, Value="XXX"}, Object { Key=9, Value="YYY"}]

我正在使用jquery客户端库.

I am working with the jquery client side library.

任何人都知道如何将对象转换为具有键/值属性的对象数组吗?

Anyone know how I can convert the object to an array of objects with Key/Value properties?

推荐答案

以下是可重用的函数,它将解决您的问题:

Here's a reusable function that'll solve your problem:

var bad = {
  7: "XXX",
  9: "YYY"
};

function fix(input) {
  var output = [];

  for (var index in input) {
    output.push({
      "KEY": index,
      "VALUE": input[index]
    });
  }

  return output;
}

// [Object { Key=7, Value="XXX"}, Object { Key=9, Value="YYY"}]
var good = fix(bad);

console.log(good)

这篇关于将对象转换为对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:20