Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Javascript nested array and filter

Here is my code

https://codepen.io/hobbesa/pen/jOGREKy?editors=1111

I am trying to get price of only weekdays.How do I do that?

 this.getPriceWeekDay = this.StrategypricingDetail.map((rec) =>
      rec.map((daysOfWeek) => {
        return daysOfWeek;
      }),

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can first, filter items which doesn't contain Saturday or Sunday by using filter and find methods, and then iterate over filtered list by map method to extract just price property, like this:

let StrategypricingDetail = [ { id: 3, name: "test", price: 30, daysOfWeek: [ { id: 1, name: "Monday" }, { id: 2, name: "Tuesday" }, { id: 3, name: "Wednesday" } ] }, { id: 23, name: "Testing2", price: 10, daysOfWeek: [ { id: 1, name: "Monday" }, { id: 2, name: "Tuesday" } ] }, { id: 13, name: "Testing3", price: 14, daysOfWeek: [ { id: 1, name: "Saturaday" }, { id: 2, name: "Sunday" } ] } ];

const weekDaysPrice = StrategypricingDetail.filter(({daysOfWeek}) => !daysOfWeek.find(({name}) => name == 'Saturaday' || name == 'Sunday')).map(({price}) => price);

console.log(weekDaysPrice);

about 4 years ago · Juan Pablo Isaza Report

0

One solution you could use below.

 let strategypricingDetail = [
      {
        id: 3,
        name: "test",
        price: 30,
        daysOfWeek: [
          { id: 1, name: "Monday" },
          { id: 2, name: "Tuesday" },
          { id: 3, name: "Wednesday" }
        ]
      },
      {
        id: 23,
        name: "Testing2",
        price: 10,
        daysOfWeek: [
          { id: 1, name: "Monday" },
          { id: 2, name: "Tuesday" }
        ]
      },
      {
        id: 13,
        name: "Testing3",
        price: 14,
        daysOfWeek: [
          { id: 1, name: "Saturaday" },
          { id: 2, name: "Sunday" }
        ]
      }
    ];
  
let finalList = [];  

// Create list of eligible days for the search
let daysToInclude = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"
];
strategypricingDetail.forEach((item, index) => {
  for (let i = 0; i < item.daysOfWeek.length; i++) {
  // Check if the day is in the eligible list
    if (daysToInclude.includes(item.daysOfWeek[i].name)) {
      finalList.push(item);      
      break;
      // When found, break out of the search after adding it to the final list.
    }
  }
});

console.log(finalList)

In this solution, you loop through the available items, and then compare each item's 'daysOfWeel' list with a list of eligible days. As soon as it finds one, it'll stop searching, and add that item to a new list, until you end with the list of appropriate days.

about 4 years ago · Juan Pablo Isaza Report

0

//StrategypricingDetail is the whole json
const StrategypricingDetail = [
  {
    id: 3,
    name: "test",
    price: 30,
    daysOfWeek: [
      { id: 1, name: "Monday" },
      { id: 2, name: "Tuesday" },
      { id: 3, name: "Wednesday" }
    ]
  },
  {
    id: 23,
    name: "Testing2",
    price: 10,
    daysOfWeek: [
      { id: 1, name: "Monday" },
      { id: 2, name: "Tuesday" }
    ]
  },
  {
    id: 13,
    name: "Testing3",
    price: 14,
    daysOfWeek: [
      { id: 1, name: "Saturaday" },
      { id: 2, name: "Sunday" }
    ]
  }
];

const weekends = ["Saturaday","Sunday"]
// Approach
// we have to filter some element based on condition
// condition will be days should not belong to weekends
this.getPriceWeekDay = StrategypricingDetail.filter((rec) => {
  for(let dayIndex = 0 ; dayIndex < rec.daysOfWeek.length; dayIndex++){
    const dayName = rec.daysOfWeek[dayIndex].name;
    if(weekends.includes(dayName)){
       return;
    }
  }
  return rec;
}).map(({price}) => price);

console.log(getPriceWeekDay);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!