0

The code looks as following:

inputArray = [
    { name: 'file1.jpg' },
    { name: 'file1.jpg' },
    { name: 'file2.jpg' },
    { name: 'file3.jpg' },
    { name: 'file4.jpg' }
]

filteringArray = ['file1', 'file2']

const outputArray = inputArray.filter( ? )

I need to filter the inputArrat so that the outputArray should contain only objects, which filenames ('name' property) are in filteringArray.

outputArray = [
    { name: 'file1.jpg' },
    { name: 'file2.jpg' }
]

I've worked so far with simpler filtering conditions, but I'm not sure how to solve this.

3
  • inputArray.filter(el => filteringArray.includes(el.name)) Commented Dec 3, 2019 at 10:52
  • 1
    .filter(img => filteringArray.includes(img.name.split('.')[0])) Commented Dec 3, 2019 at 10:53
  • Excuse me, I've made a small change in inputArray. Actually, there could be duplicates, but not in outputArray. Commented Dec 3, 2019 at 11:16

3 Answers 3

4

You can use .some() with .starsWith() to return true if the objects name starts with a file name present in your array like so:

const inputArray = [
    { name: 'file1.jpg' },
    { name: 'file2.jpg' },
    { name: 'file3.jpg' },
    { name: 'file4.jpg' }
];

const filteringArray = ['file1', 'file2'];
const outputArray = inputArray.filter(({name}) => filteringArray.some(file => name.startsWith(file)));

console.log(outputArray);

If you're looking for a solution which has a better time complexity (and has no duplicates), you could create a Map, which stores prefix file names as keys and the literal objects as values. Then, you can .map() your filteringArray using this map:

const inputArray = [
    { name: 'file1.jpg' },
    { name: 'file1.jpg' },
    { name: 'file2.jpg' },
    { name: 'file3.jpg' },
    { name: 'file4.jpg' }
]

const filteringArray = ['file1', 'file2'];
const lut = new Map(inputArray.map(({name}) => [name.split('.').shift(), {name}]));
const outputArray = filteringArray.map(f => lut.get(f));

console.log(outputArray);

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

2 Comments

Excuse me, I've made a small change in inputArray. Actually, there could be duplicates, but not in outputArray.
@tesicg does the second code snippet work for you? It produces unique elements
1

You can use combination of filter and includes!

const inputArray = [
    { name: 'file1.jpg' },
    { name: 'file2.jpg' },
    { name: 'file3.jpg' },
    { name: 'file4.jpg' }
]

const filteringArray = ['file1', 'file2']

const outputArray = inputArray.filter((person) => filteringArray.includes(person.name.split(".")[0]))

console.log(outputArray);

1 Comment

Excuse me, I've made a small change in inputArray. Actually, there could be duplicates, but not in outputArray.
1

You can use a simple function to do filter operation in the array using the filter logic array items as below.

var outputArray = [];
var inputArray = [
    { name: 'file1.jpg' },
    { name: 'file1.jpg' },
    { name: 'file2.jpg' },
    { name: 'file3.jpg' },
    { name: 'file4.jpg' }
];

var filteringArray = ['file1', 'file2'];

function filterItems(arr, query) {
  arr.filter(function(el) {
    query.forEach(function(item){
      if(el.name.toLowerCase().indexOf(item.toLowerCase()) !== -1){
        outputArray.push(el.name);
      }
    });      
  });
}

filterItems(inputArray, filteringArray);

console.log(remove_duplicates(outputArray));

function remove_duplicates(arr) {
    var obj = {};
    var ret_arr = [];
    for (var i = 0; i < arr.length; i++) {
        obj[arr[i]] = true;
    }
    for (var key in obj) {
        ret_arr.push(key);
    }
    return ret_arr;
}

NOTE: Updated the code, Now input array is having duplicate values. And in output array, there is no duplicate entry as I removed duplicates using a function (Remove duplicate values from JS array)

1 Comment

Excuse me, I've made a small change in inputArray. Actually, there could be duplicates, but not in outputArray.

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.