1

I am trying to modify the values of an object by using .fromEntries(). As you can see in the picture below, I am literally returning a modified Array of hours, but after the function ends, it returns the complete Array, instead of the updated one.

Thank you for any ideas in advance!

enter image description here


    let filteredDates = Object.fromEntries(Object.entries(slots).filter(([slot, value]) => {
        let result = datesArray.some(filterdates => {
            return filterdates.some(filter => slot === filter)
        })
        let currentDay = slot === day ? value.filter(time => time >= currentHour) : null
        result = currentDay ? currentDay : result ? value : result
        console.log("result",result)
        return result
    }))
    
    console.log("filteredDates",filteredDates)
FZs
  • 16,581
  • 13
  • 41
  • 50
Jonathan Sandler
  • 323
  • 3
  • 17
  • 1
    `filter()` doesn't return the results of the callback function. It returns the elements of the original array whenever the callback function returns a truthy value. – Barmar Nov 17 '20 at 00:27
  • 1
    Use `map()` if you want to return a new array by transforming the values from the original array. – Barmar Nov 17 '20 at 00:29
  • Thanks for the comment, the issue with that is I cannot return null, undefined, or false, else I am getting TypeError: Iterator value false is not an entry object – Jonathan Sandler Nov 17 '20 at 01:35
  • You should be returning an array of the form `[newkey, newvalue]` – Barmar Nov 17 '20 at 01:36
  • 1
    If you want to return conditionally use `reduce()`, or use `.filter(testfunction).map(conversionfunction)` – Barmar Nov 17 '20 at 01:37

1 Answers1

3

I think the closest solution to your current approach is to map first, returning null for the entries you want to delete, and then filter out the null entries to prevent Object.fromEntries from throwing. In code:

const myObj = { "a": 1, "b": 2, "c": 3 };

// Your modification
const doubleWithoutB = ([ key, value ]) => 
  key === "b"            // This is your filter logic
    ? null
    : [ key, value * 2 ] // Here's your map operation
    

console.log(
  Object.fromEntries(
    Object
      .entries(myObj)
      .map(doubleWithoutB)
      .filter(kvp => kvp !== null) // Get rid of the null kvps
  )
)

If you want to do it in one pass, you could skip Object.fromEntries and reduce the entries in to an object yourself:

const myObj = { "a": 1, "b": 2, "c": 3 };

// Your modification
const reduceDoubleWithoutB = (acc, [ key, value ]) => 
  Object.assign(
    acc,
    key === "b" 
      ? {}
      : { [key]: value * 2 }
  )
    

console.log(
  Object
    .entries(myObj)
    .reduce(reduceDoubleWithoutB, {})
)
user3297291
  • 22,592
  • 4
  • 29
  • 45
  • Thank you for your answer! I solved it using a mix of both of your answers. I removed Object.fromEntries() and I added the .filter(kvp => kvp !== null). Now works fine. Thanks Final code: ```let filteredDates = Object.entries(slots).map(([slot, value]) => { let result = datesArray.some(dates => { return dates.some(filter => slot === filter)}) let currentDay = slot === day ? value.filter(time => time >= currentHour) : null result = currentDay ? currentDay : result ? value : null return result }).filter(value => value !== null)``` – Jonathan Sandler Nov 19 '20 at 23:15