0

Basically I have an array of files (which include file names and paths etc.) and I am trying to see if a file with the same pathname already exists in the array (even if the other variables are different).

If the value is found in that array I want to move on, if it is not I want to add that object to the array. Also giving me trouble is that the array is empty at first (so I can't even run my check function the first time without checking allFiles.length).

What I am doing is looping through the entire array, and setting a Boolean to true if I find the value contained in the array, then going on to use an if...else

I thought this would be simple but I guess I was mistaken. I came up with this method and I am wondering if there is a cleaner way to do it.

  var allFiles = [];

  //function is called giving me a file, then:


            if (!allFiles.length) {
                allFiles.push(file); //Seems like there should be a better way
            } else {
                for (var i = 0; i < allFiles.length; i += 1) {
                    var exists = false;
                    if (allFiles[i]['fileName'] == fileName) {
                        exists = true;
                        console.log('Already exists');
                        break;
                    }
                }
                if (exists) {
                    console.log('Remove the file');
                    exists = false;
                } else {
                    console.log('Adding File');
                    allFiles.push(file);
                }
            }

Maybe that is clean enough, I was just wondering if you know of a different route. Thanks

2 Answers 2

1

You can pretty much use all the cool ECMA 5 array iteration methods now. They are limited to IE9+ but if you really wanted to most have shims in the mozilla documentation.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Iteration_methods

So you have a few options. If all you want is to add the object if it does not exist:

if (!allFiles.some(function(f){ return f.filename===filename}))
  allFiles.push(file);

If you need to do something with the file then you could just pull the index:

var idx = allFiles.reduce(function(m,f,idx) { 
   return m || (f.filename===filename ? idx : null)
},null);

if (var foundFile = allFiles[idx]) { ... }
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Array.some to test for the presence of a given path:

var objs = [{name: 'foo', path: 'c:\lulz'}, {name: 'foo', path: 'c:\looolz'}];

var conditionallyAddObjToArr = function (arr, obj) { 
    if (!arr.some(function (_obj) { return obj.path === _obj.path; })) {
        return [].concat(arr, obj);
    } else {
        console.log('path is already present', obj.path);
        return arr;
    }
}

objs = conditionallyAddObjToArr(objs, {name: 'baz', path: 'c:\wat'});  // will be added
objs = conditionallyAddObjToArr(objs, {name: 'baz', path: 'c:\lulz'}); // won't be added

Fiddle

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.