feature.attributes syntax

4405
6
Jump to solution
01-30-2015 12:06 PM
KeithAnderson
New Contributor III

Friends

I have some feature results coming back from a queryTask to populate a grid.

This return gives me the correct data

var data = arrayUtils.map(results.features, function (feature) {

return { "id": feature.attributes["OBJECTID"],  

         "NAME": feature.attributes["PARCELID"]

    {

});

but I would like just the first two fields without explicitly naming them.

Something like this?

return { "id": feature.attributes[outFields[0]],                 

          "NAME": feature.attributes[outFields[1]]

        }

Just can't get it.

Any syntax suggestions?

Keith

Tags (1)
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor

Thanks!

Odd it didn't work, but you could hack together that outFields array and try it the way you have in your example.

Something like:

var outFields = Object.keys(feature.attributes);
//var outFields = Object.keys(features[0].attributes); //> I would do this when you first get results
return {
  "id": feature.attributes[outFields[0]],                 
  "NAME": feature.attributes[outFields[1]]
};

If you are using an older version of IE, you could use dojox/lang/functional/object to get the keys.

It's a little odd to do it that way but I think that would definitely work. maybe

View solution in original post

6 Replies
ReneRubalcava
Frequent Contributor

A QueryTask returns a couple of pieces of info you could use. It returns a fieldAliases array and a fields array.

The fields array isn't listed in the docs, but it's on the REST spec.

Anyway, you could do something like

return { 
  "id": feature.attributes[featureSet.fields[0].name],   
   "NAME": feature.attributes[featureSet.fields[1].name]
};

It's a little odd, but that could work.

0 Kudos
KeithAnderson
New Contributor III

Thanks for the quick response Rene….but that didn’t work.

I did, however, buy your book.

I love it.

I read it every day along with the dojo Toolkit documentation.

Pretty sad life;)

Keith

0 Kudos
ReneRubalcava
Frequent Contributor

Thanks!

Odd it didn't work, but you could hack together that outFields array and try it the way you have in your example.

Something like:

var outFields = Object.keys(feature.attributes);
//var outFields = Object.keys(features[0].attributes); //> I would do this when you first get results
return {
  "id": feature.attributes[outFields[0]],                 
  "NAME": feature.attributes[outFields[1]]
};

If you are using an older version of IE, you could use dojox/lang/functional/object to get the keys.

It's a little odd to do it that way but I think that would definitely work. maybe

KeithAnderson
New Contributor III

R

U r a superstar!

It worked.

Thanks you so much.

Keith

0 Kudos
MahtabAlam1
Occasional Contributor

It's probably not working as you won't be able to read the featureSet object which is assigned outside the scope of arrayUtil.map function. So you need to pass "this" context using lang.hitch.So add module "dojo/_base/lang" as lang and try following:

featureSet = results;

var data = arrayUtils.map(results.features, lang.hitch(this,function (feature) {

return {

  "id": feature.attributes[featureSet.fields[0].name],  

    "NAME": feature.attributes[featureSet.fields[1].name]

    {

}));

KeithAnderson
New Contributor III

Thank you Mahtab!

0 Kudos