简体   繁体   中英

How to push multiple values in array?

I have one array arrayPath 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);

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

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"]]

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")) 

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM