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

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

c# - convert object to array of objects

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

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

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...