0

I'm just new at javascript and currently working with arrays and objects.

Can somebody help me to get the data if the searchble is true in columns. I have columns in array

let columns = [
 {
   data: "id",
   searchable: false
 },
 {

   data: "first_name",
   searchable: true
 },
 {
   data: "last_name",
   searchable: true
 }
];
let filter = {
  target: columns, // if searhable is true
};

If I console log the filter. It will look like this. Result:

{
target: [
  0: "first_name"
  1: "last_name"
  ]
}

2 Answers 2

1

A one-liner solution, with Array.filter() and Array.map().

const columns = [
 {
   data: "id",
   searchable: false
 },
 {

   data: "first_name",
   searchable: true
 },
 {
   data: "last_name",
   searchable: true
 }
];

const filter = {
  target: columns,
};

const result = columns.filter(column => column.searchable).map(column => column.data);
filter.target = result;

console.log(filter);

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

Comments

0

const columns = [{
    data: "id",
    searchable: false
  },
  {

    data: "first_name",
    searchable: true
  },
  {
    data: "last_name",
    searchable: true
  }
];


let filter = {
  target: []
};

for (let i = 0; i < columns.length; i++) {
  if (columns[i].searchable) {
    filter.target.push(columns[i].data);
  }
}
console.log(filter);

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.