0

I have an array like this .

{
  "filters": [
   
    {
      "filterProperty": "companyType",
      "filterValues": [
        "Private"
      ]
    },
    {
      "filterProperty": "city",
      "filterValues": [
        "Mumbai",
        "SanJose",
        "Shanghai"
      ]
    }
    
  ]
}

I have applied Filters. now I am removing them One by One and calling an API with remaining Filters.

so when I pass filterProperty as "city" and value as "Mumbai" then only "Mumbai" would remove from filterProperty "city" . the rest filter values should be same.

How can I do so ?

  "filters": [
   
    {
      "filterProperty": "companyType",
      "filterValues": [
        "Private"
      ]
    },
    {
      "filterProperty": "city",
      "filterValues": [
        "Mumbai",
        "SanJose",
        "Shanghai"
      ]
    }
    
  ]


let data = filters.splice(a=> a.filterProperty === 'city' && a.filterValues.filter(a => a == "Mumbai"))
William Wang
  • 8,051
  • 3
  • 5
  • 28
Xperia Reno
  • 161
  • 1
  • 12
  • The [`splice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) method does not take a function... – Heretic Monkey Feb 05 '21 at 17:29
  • Does this answer your question? [How can I remove a specific item from an array?](https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array) – Heretic Monkey Feb 05 '21 at 17:30
  • that doesn't solve my answer. thank you – Xperia Reno Feb 05 '21 at 17:38
  • Sure it does. `const filter = filters.find(f => f.filterProperty === 'city'); filter.filterValues.splice(filter.filterValues.indexOf('Mumbai'), 1);` – Heretic Monkey Feb 05 '21 at 17:43

3 Answers3

0

I assume you need map along with filter. Something like this:

const obj = { "filters": [ { "filterProperty": "companyType", "filterValues": [ "Private" ] }, { "filterProperty": "city", "filterValues": [ "Mumbai", "SanJose", "Shanghai" ] } ]};

const result = obj.filters.map(k=>(k.filterProperty == "city" ? (k.filterValues = k.filterValues.filter(o=>o!=='Mumbai'), k) : k ));

console.log(result);
gorak
  • 4,839
  • 1
  • 4
  • 16
0

Use Array.reduce()

let filters=[{filterProperty:"companyType",filterValues:["Private"]},{filterProperty:"city",filterValues:["Mumbai","SanJose","Shanghai"]}];

let data = filters.reduce((acc,cur) => {
  if(cur.filterProperty === "city"){
    cur.filterValues.splice(cur.filterValues.indexOf("Mumbai"),1)
  }
  acc.push(cur)
  return acc
},[])

console.log(data)
symlink
  • 10,400
  • 6
  • 25
  • 47
0

Without mutating the data, use forEach and filter build new array of items.

const filter = (obj, prop, value) => {
  const filters = [];
  obj.filters.forEach(({ filterProperty, filterValues }) => {
    filters.push({
      filterProperty,
      filterValues: filterValues.filter((item) =>
        filterProperty === prop ? item != value : true
      ),
    });
  });
  return {
    filters,
  };
};

const data = {
  filters: [
    {
      filterProperty: "companyType",
      filterValues: ["Private"],
    },
    {
      filterProperty: "city",
      filterValues: ["Mumbai", "SanJose", "Shanghai"],
    },
  ],
};

const prop = "city";
const value = "Mumbai";

console.log(filter(data, prop, value));

Solution with mutating the data, using find and splice

const data = {
  filters: [
    {
      filterProperty: "companyType",
      filterValues: ["Private"],
    },
    {
      filterProperty: "city",
      filterValues: ["Mumbai", "SanJose", "Shanghai"],
    },
  ],
};

const prop = "city";
const value = "Mumbai";

const city = data.filters.find(({ filterProperty }) => filterProperty === prop);
if (city) {
  const index = city.filterValues.findIndex((item) => item === value);
  if (index > -1) {
    city.filterValues.splice(index, 1);
  }
}

console.log(data);
Siva K V
  • 7,622
  • 2
  • 11
  • 24