0

There has to be a better way of writing the js in my function....

I need to end up with bare json that looks like this [1095365, 1125504].

Setup of code example;

var article = {"Id":0,"dirtyFlags":null,"textAreas":null,"metaData":null,"relatedCIs":[{"Id":1095365,"Type":4},{"Id":1125504,"Type":4}],"clients":[{"Id":992754}]};
myFunction(article.relatedCIs);

and here's the function i want to optimise;

function myFunction(jRelatedCIs) {
  var idArray = [];
  $.each(jRelatedCIs, function (i, ci) {
    idArray.push(ci.Id);
  });

  var jIdArray = $.toJSON(idArray);

  ....other code
}

1 Answer 1

1

What's suboptimal about what you're doing at the moment? Maybe "optimise" wasn't the right word :)

You could avoid the need for the extra id array by doing it as a one-liner using map, but it's still doing roughly the same thing under the hood:

var jIdArray = $.toJSON($.map(jRelatedCIs, function(ci){return ci.Id;}));
Sign up to request clarification or add additional context in comments.

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.