1

I have one arrayarrayPath I push multiple values in arrayPath Which appears in the following code singleFile.originalFilename has duplicate(which is shown in console) values, when singleFile.originalFilename duplicate values that duplicate value I dont want to push in arrayPath how it is possible ?

I tried with below code but i not get right result.

var arrayPath = [];
console.log(singleFile.originalFilename); // '3.jpg','5.jpg','3.jpg','3.jpg','8.jpg'
console.log(singleFile.size);  // '1345','5778','1345','1345','7777'

for(i=0; i < files.uploadFiles.length; i++){

  singleFile=files.uploadFiles[i];
  if(arrayPath.indexOf(singleFile.originalFilename) === -1){
    arrayPath.push([singleFile.originalFilename,singleFile.size,'true']);
  }else{
    console.log("some singleFiles are duplicate");
  }
}
console.log(arrayPath);
4
  • is singleFile is an object ? Commented Dec 4, 2018 at 11:02
  • 1
    Possible duplicate of How to merge two arrays in JavaScript and de-duplicate items Commented Dec 4, 2018 at 11:07
  • @DILEEPTHOMAS it is array Commented Dec 4, 2018 at 11:41
  • @Ankit please check the answer, if i missed the singleFile structure of data, please post the array so i can update the answer. Commented Dec 4, 2018 at 11:52

4 Answers 4

2

You can use a Set to make sure you only have unique values.

const x = new Set([1,2,2,4,5,5])
console.log([...x.values()]) // [1,2,4,5]

You can read more in the Mozilla Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

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

3 Comments

If you dont want to use sets you have to write a unique filter or filter just in time during pushing your values. function unique(values) {return values.filter(function (value, index, self) { return self.indexOf(value) === index;});}
OP is pushing array to the arrayPath. Set cannot be used in such cases! x = new Set([[1], [1], [1]]) will have 3 items in the set.
@Errorname const x = new Set([singleFile.originalFilename]) i doing this but console is like this it not remove duplicate console.log([...x.values()]) // ['8.jpg'],['8.jpg']
1

Assuming, the files has following structure, you can do:

const files = {
  uploadFiles: [{originalFilename: '3.jpg', size: 1345}, {originalFilename: '5.jpg', size: 5778}, {originalFilename: '3.jpg', size: 1345}, {originalFilename: '3.jpg', size: 1345}, {originalFilename: '8.jpg', size: 7777}]
};
const uniqueFilesMap = files.uploadFiles.reduce((map, current) => {
  map.set(current.originalFilename, [current.originalFilename, current.size, 'true']);
  return map;
}, new Map());
const result = [...uniqueFilesMap.values()]; // [["3.jpg",1345,"true"],["5.jpg",5778,"true"],["8.jpg",7777,"true"]]

Comments

1

i hope you have an array of objects as shown below, where you can write a function which accepts the array and a property.

Based on the property you can filter the array. so by checking you can remove the duplicates and return a new array. we can use array.filter

I hope the below code will solve the issue. Since its a function you can use it multiple times and you can filter any property in the array.

let singleFile = [{fileName: "3.jpg", size: 1234}, {fileName: "4.jpg", size: 1236},
{fileName: "5.jpg", size: 1237},
{fileName: "3.jpg", size: 1234},
{fileName: "3.jpg", size: 1234}]

// method accepts array and property
// you can check with property on the array and remove  duplicates from array and return a new array
const removeDuplicates = (array, property) => {
 let uniq = {}
 return array.filter(obj => !uniq[obj[property]] && (uniq[obj[property]] = true))
}


console.log(removeDuplicates(singleFile, "fileName"))

2 Comments

TypeError: array.filter is not a function this type error i get
so can you post the array that you are passing to the removeDuplicates function ?
0

You need to have code like this.

var arrayPath = [];
console.log(singleFile.originalFilename); // '3.jpg','5.jpg','3.jpg','3.jpg','8.jpg'
console.log(singleFile.size);  // '1345','5778','1345','1345','7777'

for(i=0; i < files.uploadFiles.length; i++){

  singleFile=files.uploadFiles[i];
  if(!this.isArrayDataUnique(singleFile.originalFilename)){
    let obj = {
      filename: singleFile.originalFilename,
      size: singleFile.size,
      status: 'true'
    };
    arrayPath.push(obj);
  }else{
    console.log("some singleFiles are duplicate");
  }
}
console.log(arrayPath);

function isArrayDataUnique(originalFileName){

 let filters = arrayPath.filter(item => item.filename === originalFileName);
 return filters.length;
}

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.