javascript - How to push multiple values in array? - Stack Overflow

I have one arrayarrayPath I push multiple values in arrayPath Which appears in the following code singl

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

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);
Share Improve this question edited Dec 4, 2018 at 10:53 Code Maniac 37.8k5 gold badges43 silver badges64 bronze badges asked Dec 4, 2018 at 10:46 DharmeshDharmesh 6,02318 gold badges49 silver badges67 bronze badges 4
  • is singleFile is an object ? – Beginner Commented Dec 4, 2018 at 11:02
  • 1 Possible duplicate of How to merge two arrays in JavaScript and de-duplicate items – DarkMukke Commented Dec 4, 2018 at 11:07
  • @DILEEPTHOMAS it is array – Dharmesh 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. – Beginner Commented Dec 4, 2018 at 11:52
Add a ment  | 

4 Answers 4

Reset to default 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/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;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744920538a4601105.html

相关推荐

  • javascript - How to push multiple values in array? - Stack Overflow

    I have one arrayarrayPath I push multiple values in arrayPath Which appears in the following code singl

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信