0

I'm trying to convert this code I wrote into chained functions since I thought it would look cleaner.

let z =this.parkinglot.filter(val=>{
           let propArr =  Object.entries(val.spaces);
           let pass = propArr.some(w=>{
                let [key,value] = w;
                return (value > 0);
                })   
            return (pass===true); 
          })

My data structure is an array of objects that looks like this (Stored in an array called this.parkinglot) enter image description here

When I attempt to rewrite this as chained function, I run into the problem of giving the proper input to my functions. Since I need the inner data I needed to use a map but now I have an 3 Layered Array.

Then I ran into the problem of how would I then give this data to filter properly.

let t = this.parkinglot.map(val => Object.entries(val.spaces))
  .some(w=>{
    console.log('aa',w);
      // let [key,value]=w
       //return (value > 0);
  })
  //.filter(z=>z>1)
6
  • 3
    Please don't post images of text--put the actual structure as a copy-and-pasteable text block in the question. Otherwise, we have to type this in by hand. Also, I recommend just directly return arr.some... instead of let pass = arr.some..; return pass===true. Commented Aug 27, 2019 at 17:14
  • 2
    some returns boolean value so can't chain them like this Commented Aug 27, 2019 at 17:14
  • 3
    To make cleaner first thing you should remove here (pass === true), instead just use pass Commented Aug 27, 2019 at 17:17
  • 2
    Use Object.values, not Object.entries Commented Aug 27, 2019 at 17:18
  • 2
    There really is no way to chain this Commented Aug 27, 2019 at 17:19

1 Answer 1

2

Closest you can get to chaining it is a filter and inside a some. And since you only care about the values, use Object.values instead of entries.

var data = [
  { level: 1, spaces : {a: 0, b:0, c: 0} },
  { level: 2, spaces : {a: 2, b:0, c: 0} },
  { level: 3, spaces : {a: 0, b:3, c: 0} },
  { level: 4, spaces : {a: 0, b:0, c: 10} },
  { level: 5, spaces : {a: 0, b:0, c: 0} },
]

const open = data.filter(level => Object.values(level.spaces).some(v => v))

console.log(open);

If you want the levels you can just add map() to the end

.filter(...).map(data => data.level)
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.