-3

I'm new to JavaScript so my array mapping skills are bad, how would I find the assetid which is 47243781293 in this array? Thank you.

    EconItem {
  appid: 440,
  contextid: '2',
  assetid: '4723781293',
  classid: '2674',
  instanceid: '11040547',
  amount: 1,
  missing: false,
  currency: false,
  background_color: '3C352E',
  icon_url: '...',
  icon_url_large: '...',
  tradable: false,
  actions: 
   [ { link: 'http://wiki.teamfortress.com/scripts/itemredirect.php?id=5002&lang=en_US',
       name: 'Item Wiki Page...' } ],
  name: 'Refined Metal',
  name_color: '7D6D00',
  type: 'Level 3 Craft Item',
  market_name: 'Refined Metal',
  market_hash_name: 'Refined Metal',
  commodity: false,
  market_tradable_restriction: 7,
  market_marketable_restriction: 0,
  id: '4723781293',
  fraudwarnings: [],
  descriptions: [],
  owner_descriptions: [],
  owner_actions: [],
  tags: [],
  marketable: false 
}
Sylvite
  • 39
  • 5
  • what do you mean how to get, as in the value? did you want to get any value from your JSON? – Malcolm Salvador Sep 28 '17 at 01:07
  • How would I get the assetid value from this array? – Sylvite Sep 28 '17 at 01:07
  • What you posted is an object, not an array. Are you just trying to extract the assetid from this object? Or is there an array with multiple objects like this, and you're trying to find the object who's assetid == '47243781293' – Nicholas Tower Sep 28 '17 at 01:08
  • `EconItem.assetid`. That's an Object, by the way. – StackSlave Sep 28 '17 at 01:09
  • Opps yes I'm trying to find the assetid value in this object that you see now. – Sylvite Sep 28 '17 at 01:09
  • Your question is not very clear. If you are just looking to access the `assetid` value if you have the given object, `EconItem.assetid` should do it. As a previous poster said, if you have an array of objects of that type, and you are looking for a specific object, then you have to do something else. – Ken H. Sep 28 '17 at 01:10
  • If this was object was in an array, how would I find it? – Sylvite Sep 28 '17 at 01:12
  • check my answer, have showed how if it's an array – Theophilus Omoregbee Sep 28 '17 at 01:17

3 Answers3

0

I think you mean to ask "..which is 47243781293 in this JSON" and not "...which is 47243781293 in this array". The object which you pasted above is a JSON representation. If that's what you are meaning to ask, please read below -

Given that the key id will always be present and the object value pasted above is assigned to variable EconItem, I would try something like this

If( EconItem['id'] === '47243781293' )
{
   console.log('Asset id: 47243781293 present in the JSON object');
}

If you are not sure that key id will always be present in the object, I would first check for the key to be present using Object.keys(). Details can be found here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Hope that helps!

Rohan Rayarikar
  • 240
  • 2
  • 6
0

To get a value from the object, just refer to it's key: EconItem.assetid

For objects with multiple results, You can iterate on it, and output a particular value from your Object like so:

for (var i in EconItem)
{
    console.log(EconItem[i].assetid);
    //do more here
}
Malcolm Salvador
  • 1,476
  • 2
  • 21
  • 40
0

to find this in an array, you use filter which is a function that ships with every javascript array, using your object above for each EconItem

  let array = [EconItem, EconItem]

   search = array.filter(eachItem=>eachItem.assetId==='47243781293');
--> returns an array of items with assetId as 47243781293, now the first item should be your EconItem, i.e search[0];

Check example snippet

var items = [{
  appid: 440,
  contextid: '2',
  assetid: '4723781293'
}, {
  appid: 441,
  contextid: '2',
  assetid: '4723781292'
}];


// now we search with this 4723781293 
var search = items.filter(function(item){
return item.assetid === '4723781293';
});

//show our search result
alert ("item appid is:"+search[0].appid+", context:"+search[0].contextid+", assetid:"+search[0].assetid);
Theophilus Omoregbee
  • 2,463
  • 1
  • 22
  • 33