7

Assuming I have 2 JSON Object arrays, which looks like this:

Resources:

[{
 "DefinitionId": 193041,
 "ResourceId": -2147290607,
 "AssetId": 193041
}, {
 "DefinitionId": 193042,
 "ResourceId": -2147290603,
 "AssetId": 193042
}]

ResourceIds

[193041, 193041, 193041, 193042]

The use-case:

I need to list the details from my Resources JSONObject for each ResourceId. For example I want to output the AssetId for every ResourceId in ResourceIds.

My plan:

I thought it would be an elegant solution to convert my Resources JSON into an associative array, so that I could access the AssetId for my ResourceId '193041' like this: Resources[193041].AssetId . The problem: I could only think about long code to convert my above Resources JSON into an associative JSON object.

The question:

How can I convert the above Resources JSON object array into an associative object array with ResourceId as key?

Desired Resources.json:

{
 "-2147290607": {
 "DefinitionId": 193041,
 "ResourceId": -2147290607,
 "AssetId": 193041
 },
 "-2147290603": {
 "DefinitionId": 193042,
 "ResourceId": -2147290603,
 "AssetId": 193042
 }
}
asked Jul 30, 2016 at 18:23
3
  • 1
    Could you show desired result? Commented Jul 30, 2016 at 18:26
  • @NenadVracar sure, edited! Commented Jul 30, 2016 at 18:29
  • btw, JSON is a serialized object in string form. Commented Jul 30, 2016 at 18:36

3 Answers 3

2

You could use an object and iterate the array with Array#forEach

The forEach() method executes a provided function once per array element.

and assign the element to the property with the name of a.ResourceId.

The callback uses an Arrow function, because there is only one assignment.

var data = [{ "DefinitionId": 193041, "ResourceId": -2147290607, "AssetId": 193041 }, { "DefinitionId": 193042, "ResourceId": -2147290603, "AssetId": 193042 }],
 object = {};
data.forEach(a => object[a.ResourceId] = a);
console.log(object);

answered Jul 30, 2016 at 18:31
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, do you mind to elloborate this a bit: data.forEach(a => object[a.ResourceId] = a); ?
1

You can use reduce:

var resources = [{
 "DefinitionId": 193041,
 "ResourceId": -2147290607,
 "AssetId": 193041
}, {
 "DefinitionId": 193042,
 "ResourceId": -2147290603,
 "AssetId": 193042
}];
var resourceIds =[193041, 193041, 193041, 193042];
var res = resources.reduce( function(prev, curr) {
 // Check AssetId
 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
 if ( resourceIds.indexOf( curr.AssetId ) >= 0 ) prev[ curr.ResourceId ] = curr;
 return prev;
}, {} );
var resJSON = JSON.stringify( res );
answered Jul 30, 2016 at 18:48

Comments

0
var Resources = [{
 "DefinitionId": 193041,
 "ResourceId": -2147290607,
 "AssetId": 193041
}, {
 "DefinitionId": 193042,
 "ResourceId": -2147290603,
 "AssetId": 193042
}]; 
Resources.find(function(value){return value.ResourceId === -2147290603}).AssetId

or use lodash/underscore for an elegant solution: _.find(Resources, {ResourceId : -2147290603}).AssetId;

With this we can find the required AssetId by just passing the ResourceId. We can even skip the conversion of JSON for simplicity.

answered Jul 30, 2016 at 19:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.