Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
467 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...