0

No jQuery, only JS.

var array1 = [
  {
    key1: '01',
    key2: 'abc',
    key3: 'def'
  },
  {
    key1: '02',
    key2: 'skip',
    key3: 'skip'
  },
  {
    key1: '03',
    key2: 'ghi',
    key3: 'jkl'
  },
];

var filters = ['ab', 'kl'];

function filter_by_part_of_string(array, filters) {
  return ...
}

I would like to get

[
  {
    key1: '01',
    key2: 'abc',
    key3: 'def'
  },
  {
    key1: '03',
    key2: 'ghi',
    key3: 'jkl'
  },
];

I would like to filter by just part of string in value at any key. I tried search on stackoverflow, but most of answer i found was just filter by string but not a array and part of string.

2
  • 2
    Can you please add the code that you tried? Commented May 14, 2020 at 10:03
  • Will your array objects always contain only 3 key? Commented May 14, 2020 at 10:05

6 Answers 6

2

Another option is to use some that will return once an element fulfils criteria:

const filter = (terms, arr) => {
    return arr.filter(obj => Object.values(obj).some(val => {
        return terms.some((term) => val.includes(term))
   }))
};
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

function filter_by_part_of_string(array, filters) {
 return array1.filter(obj =>{
  const values =  Object.values(obj).join(' ')
  return filters.filter(f => values.indexOf(f) > -1).length
 })
}

Comments

0

Maybe it's presented not in the best way. But it works for now)

var array1 = [
  {
    key1: '01',
    key2: 'abc',
    key3: 'def'
  },
  {
    key1: '02',
    key2: 'skip',
    key3: 'skip'
  },
  {
    key1: '03',
    key2: 'ghi',
    key3: 'jkl'
  },
];

var filters = ['ab', 'kl'];

const filterByPart = (array, parts) => {
  let filtered = []  
  array.filter(element => {
    const asArray = Object.values(element)
    asArray.filter(word => parts.filter(part => word.includes(part) && filtered.push(element)))
  })
  return filtered
}

const filtered = filterByPart(array1, filters)
console.log(filtered)

Comments

0

here is a solution:

function filter_by_part_of_string(array, filters) {

  function test(obj) {
    let values = Object.values(obj);

    for (let value of values)
      for (let item of filters) {
        if (value.includes(item)) return true;
      }

    return false;
  }

  return array.filter(test);
}

Comments

0

I hope I've been helpful

var array1 = [
    {
        key1: '01',
        key2: 'abc',
        key3: 'def'
    },
    {
        key1: '02',
        key2: 'skip',
        key3: 'skip'
    },
    {
        key1: '03',
        key2: 'ghi',
        key3: 'jkl'
    },
];


var filters = ['ab', 'kl'];

var searchBy = "ab";
var array2 = [];
for (var i = 0; i < array1.length; ++i) {
    for (var key of Object.keys(array1[i])) {
        for (var ii = 0; ii < filters.length; ++ii) {
            var str = array1[i][key];
            var n = str.search(filters[ii]);
            if (n > -1) { array2.push(array1[i]) };
        }
    }
}

console.log(array2);

Comments

0

You can filter and check with Regular Expression:

var array1 = [
  {
    key1: '01',
    key2: 'abc',
    key3: 'def'
  },
  {
    key1: '02',
    key2: 'skip',
    key3: 'skip'
  },
  {
    key1: '03',
    key2: 'ghi',
    key3: 'jkl'
  }
];

var filters = ['ab', 'kl'];

const regex = RegExp(filters.join('|'));

const arr = array1.filter(({ key2, key3 }) => regex.test(key2) || regex.test(key3))

console.log(arr);

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.