3

I have an array (Dates) of 600 items like this: [{Case: 1, Date: 01012019},{Case: 2, Date: 01022019},{Case: 3, Date: 01032019}

What is the best approach to get the value from the key "Date" based on the key "Case"?

I know I can do a simple loop ($.each())on the array and break out of the loop once i find the key I'm looking for. Another option - as far as I've read at least - is to use the $.map() function.

I will need to lookup the key for 50-100 items on page load, so looping the array (Dates) seems like a lot of work to get me there. Is there a more efficient way? Og talking javescript/Jquery here :)

2
  • I assume the Case numbers are just placeholders right, they're not actually in predictable order in your real code? Commented May 9, 2019 at 6:13
  • Yup case numbers are in random order :) Commented May 9, 2019 at 6:20

2 Answers 2

1

Transform the array into an object indexed by Case first, and then instead of using a for loop or .find to find the associated object each time, just use object property lookup to find the associated object:

const dates = [{
  Case: 1,
  Date: 01012019
}, {
  Case: 2,
  Date: 01022019
}, {
  Case: 3,
  Date: 01032019
}];

const datesByCase = dates.reduce((a, item) => {
  a[item.Case] = item;
  return a;
}, {});

// now say we want to find objects associated with case 2 and 3:
console.log(datesByCase['2']);
console.log(datesByCase['3']);

For N Cases to find, this has O(N) complexity overall. (Using a for loop for every Case you want to find has O(N^2) complexity overall)

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks - this is an area I'm not really been using so forgive me, if it's a dumb question but I only need to do this once right? Also what does "const" mean in this context?
const is very similar to var, except that it's more predictable and easier to read - it has block scope, and indicates that the variable in question will not be reassigned. Yes, you only need to reduce into an object once (as you can see on the final two lines, you can look up datesByCase as many times as you want after the object is created)
@CertainPerformance will converting it to a Map make the access faster ?
@KunalMukherjee No - both Maps and objects have O(1) property lookup complexity (at least in any remotely sane implementation)
What am I doing wrong when this doesnt work (internalCases is the array) const modifiedByCase = internalCases.reduce((a, item) => { a[item.Case] = item; return a; }, {});
|
0

If cases are unique try to convert array into object, that help you to avoid looping every time:

convert like this :

const dates = [{
  Case: 1,
  Date: 01012019
}, {
  Case: 2,
  Date: 01022019
}, {
  Case: 3,
  Date: 01032019
}];
let obj = {};
dates.map((date)=>obj[date.Case]=date.Date);
console.log(obj);
console.log('case "1" date is '+obj["1"]);

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.