0

I have a returned object list: data.d[15]

and one sample of it:

data.d[3] = {
CityId: 2,
CityName: "Ankara"}

I want to convert it to one object as

cities{
1: "Istanbul",
2: "Ankara",
3: "New York"
}

And it should be generic, so I dont know the "CityId" and "CityName" field names. what is the best method for it?

thank you all... i have send fieldnames by field object -no dependencies important for this code-, it has been resolved.

            var url = this.options.url + "/" + field.values,
                id = field.fieldId,
                title = field.fieldTitle;

            this.getJSON(url, {}, function (rData) {
                var obj = {};

                for (i = 0; i < rData.d.length; i++)
                    obj[rData.d[i][id]] = rData.d[i][title];

                $("#" + parentId).html(self.getHtmlOfFormData(type, obj));
            });
6
  • if you don't know CityName, then how are we supposed to know what to project? Commented Jun 7, 2012 at 12:05
  • 1
    I can't really see a correlation between those two pieces of data and how it can be generic. Perhaps you need to be more specific. Commented Jun 7, 2012 at 12:05
  • I just want to get first value as ID and second value as Val without FieldName dependency. Commented Jun 7, 2012 at 12:10
  • @kirlisakal There is no "first" or "second" value in an object. The order of the keys depends on the implementation of the browser. Commented Jun 7, 2012 at 12:21
  • @kirlisakal Unfortunately, you can't be sure about the order of the properties when you iterate on them. You need to check their type. Commented Jun 7, 2012 at 12:25

2 Answers 2

2

Maybe you need to detect which property contains the name of the city. Maybe something like this can work?

var idprop, nameprop;
for (var prop in data.d[0]) {
    if (typeof data.d[0][prop] === "string") nameprop = prop;
    if (typeof data.d[0][prop] === "number") idprop = prop;
}

var cities = {};
for (var i = 0; i < data.d.length; i++)
   cities[data.d[i][idprop]] = data.d[i][nameprop];

Keep in mind that this works if:

  1. data.d isn't an empty array;
  2. there's just one string property that contains the city name;
  3. there's just one numeric property that contains the city id.
Sign up to request clarification or add additional context in comments.

3 Comments

i + 1 should be data.d[i].CityId
@Chango Maybe, but we need confirmation from kirlisakal
hey thank you much... I have solved the problem as I get the fieldnames by variant, cuz id's type is not allways numeric.
1

if i understood your question, you are trying to convert some ajax return that looks like this:

data.d = [
    {
        cityId: someNumber1,
        cityName: someName1
    },
    {
        cityId: someNumber2,
        cityName: someName2
    }
];

into an object that looks like this:

cities = {
    someNumber1: someName1,
    someNumber2: someName2
};

a snippet like this would do the trick:

var cities = {};
for (var i = 0; i < data.d.length; i++) {
    cities[data.d[i].cityId] = data.d[i].cityName;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.