3

The following object is used as a starting point to populate options of a select box in the UI of an app:

const months = {
  "1": "Jan",
  "2": "Feb",
  "3": "Mar",
  "4": "Apr",
  "5": "May",
  "6": "Jun",
  "7": "Jul",
  "8": "Aug",
  "9": "Sep",
  "10": "Oct",
  "11": "Nov",
  "12": "Dec"
}

The options for the select box must be limited though based on the contents of another array. Here is an example of that array:

const existingMonths = [
  1,
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  12
];

So in this example, the object of final options for the select box should be:

const availableMonths = {
  "9": "Sep",
  "10": "Oct",
  "11": "Nov"
}

I'm having difficulty figuring out how to build the availableMonths object. This does not give the desired output:

const availableMonths = Object.entries(months).filter(k => !existingMonths.includes(k));
1
  • k is an array with two entries - the key first, the value second. If you want to check if the key exists in another array, you need to just take the first element. Also, the key would be a string, while the existingMonths array contains numbers. You need to convert the two to the same type. Commented May 18, 2021 at 14:20

3 Answers 3

4

You need to take the key from the array of entries and check with the numerical value of the key against the value of the array. Finally create a new object from the entries.

const
    months = { 1: "Jan", 2: "Feb", 3: "Mar", 4: "Apr", 5: "May", 6: "Jun", 7: "Jul", 8: "Aug", 9: "Sep", 10: "Oct", 11: "Nov", 12: "Dec" },
    existingMonths = [  1,  2,  3,  4,  5,  6,  7,  8,  12],
    availableMonths = Object.fromEntries(Object
        .entries(months)
        .filter(([k]) => !existingMonths.includes(+k))
    );

console.log(availableMonths);

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

2 Comments

What does the +k do?
@knot22 parsing string to number
1

const months = {
  "1": "Jan",
  "2": "Feb",
  "3": "Mar",
  "4": "Apr",
  "5": "May",
  "6": "Jun",
  "7": "Jul",
  "8": "Aug",
  "9": "Sep",
  "10": "Oct",
  "11": "Nov",
  "12": "Dec"
}

const existingMonths = [1, 2, 3, 4, 5, 6, 7, 8, 12]

const availableMonths = Object.fromEntries(
   Object.entries(months).filter(([k, v]) => !existingMonths.includes(+k))
)

console.log(availableMonths)

Comments

1

Step 1: Shallow Copy months object
Step 2: Loop throught existingMonths
Step 3: Delete shallow copied properties by comparing with existingMonths elements

let
months = { 1: "Jan", 2: "Feb", 3: "Mar", 4: "Apr", 5: "May", 6: "Jun", 7: "Jul", 8: "Aug", 9: "Sep", 10: "Oct", 11: "Nov", 12: "Dec" },
existingMonths = [  1,  2,  3,  4,  5,  6,  7,  8,  12],availableMonths = {}

Object.assign(availableMonths,months);

existingMonths.forEach((monthNumber) =>{delete availableMonths[monthNumber]})
   

console.log(availableMonths);

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.