0

The thing that I am doing for now is not giving the correct answer.

As a result, I am getting just the value of brr array:

["model/model.yaml", 
 "model/storage-complextypes.yaml", 
 "model/storage-simpletypes.yaml"]

Trying to find out where I have made a mistake

let arr = [{
  id: "862d-8f99-4638-ef1d",
  data: "---↵  inventory:↵  - devices:↵      platformtypes:…          extract:↵            path: create_time↵",
  path: "inventory/inventory.yaml"
}, {
  id: "ccad-2ea5-1241-4d1a",
  data: "---↵  model:↵    services:↵    - name: nano↵      …erdelete: cascade↵          apiaccess: readonly↵↵",
  path: "model/model.yaml"
}, {
  id: "5f6d-2ce9-a5e4-dee8",
  data: "---↵  model:↵    packages:↵    - name: storage↵   …olicy rules.↵          type: Collection(string)↵↵",
  path: "model/storage-complextypes.yaml"
}]

const brr = ["model/model.yaml", "model/storage-complextypes.yaml", "model/storage-simpletypes.yaml"]


const res = brr.filter((i) => arr.filter((j) => j.path !== i))

console.log(res)

2
  • 2
    your inner filter() returns an array which is truthy and so all elements are included in the outer filter(). Commented Apr 21, 2021 at 11:47
  • 1
    For your inner function you'll probably want either .every() or .some() Commented Apr 21, 2021 at 11:48

1 Answer 1

1

Do you mean this?

let arr = [{
  id: "862d-8f99-4638-ef1d",
  data: "---↵  inventory:↵  - devices:↵      platformtypes:…          extract:↵            path: create_time↵",
  path: "inventory/inventory.yaml"
}, {
  id: "ccad-2ea5-1241-4d1a",
  data: "---↵  model:↵    services:↵    - name: nano↵      …erdelete: cascade↵          apiaccess: readonly↵↵",
  path: "model/model.yaml"
}, {
  id: "5f6d-2ce9-a5e4-dee8",
  data: "---↵  model:↵    packages:↵    - name: storage↵   …olicy rules.↵          type: Collection(string)↵↵",
  path: "model/storage-complextypes.yaml"
}]

const brr = ["model/model.yaml", "model/storage-complextypes.yaml", "model/storage-simpletypes.yaml"]

const paths = arr.map(({path}) => path)
const res = brr.filter(path => paths.includes(path))

console.log(res)

Other way around:

let arr = [{
  id: "862d-8f99-4638-ef1d",
  data: "---↵  inventory:↵  - devices:↵      platformtypes:…          extract:↵            path: create_time↵",
  path: "inventory/inventory.yaml"
}, {
  id: "ccad-2ea5-1241-4d1a",
  data: "---↵  model:↵    services:↵    - name: nano↵      …erdelete: cascade↵          apiaccess: readonly↵↵",
  path: "model/model.yaml"
}, {
  id: "5f6d-2ce9-a5e4-dee8",
  data: "---↵  model:↵    packages:↵    - name: storage↵   …olicy rules.↵          type: Collection(string)↵↵",
  path: "model/storage-complextypes.yaml"
}]

const brr = ["model/model.yaml", "model/storage-complextypes.yaml", "model/storage-simpletypes.yaml"]


const res = arr.filter(({path}) => brr.includes(path))

console.log(res)

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

2 Comments

Sir, I need to filter brr array by the "path" property of arr array
@mplungjan A minor thing. You don't need Object.values(arr) (used to get the paths array) since arr is an array. const paths = arr.map(({ path }) => path).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.