1

Here data console and this data push in duplicatePushArray but it once pushed I dont want to push again but if I push again i want to show same message second time also.

Ex. first time I push 2.jpg,3.jpg now again I push 2.jpg,3.jpg so it shows message like this 2.jpg,3.jpg value is already pushed please change this values now again I push 2.jpg,3.jpg so it shows message like this 2.jpg,3.jpg,2.jpg,3.jpg value is already pushed please change this values but i want to show message like this 2.jpg,3.jpg value is already pushed please change this values

console.log(data); // (2) [{…}, {…}]0: {imageName: "2.jpg"}1: {imageName: "3.jpg"}
duplicatePushArray : any[] = [];
constructor(private snackBar : MatSnackBar) {}      

for(var i = 0; i < data.length ; i++){
  if(this.duplicatePushArray.indexOf(data[i].imageName)) {
    this.duplicatePushArray.push(data[i].imageName);
    this.snackBar.open(this.duplicatePushArray+' '+ 'value is already pushed please change this values', '',{
      duration: 2000
    })
  }
}
10
  • codecraft.tv/courses/angular/es6-typescript/mapset Commented Dec 3, 2018 at 4:54
  • use if(this.duplicatePushArray.indexOf(data[i].imageName) === -1) { Commented Dec 3, 2018 at 4:55
  • @TamilSelvanC yes it not allow to push second time but i want to show message always when try to push again Commented Dec 3, 2018 at 4:59
  • I think you can use includes instead of indexOf or change the condition checking as @TamilSelvanC suggested Commented Dec 3, 2018 at 5:00
  • Do you want to push even if there a duplicate imageName? Commented Dec 3, 2018 at 5:00

1 Answer 1

3

const data = [1,2,3,4,5,6,7,1,2,3,4,1,2];
let duplicatePushArray = [];
for(let i = 0; i < data.length ; i++){
  if(duplicatePushArray.indexOf(data[i]) === -1) {
    duplicatePushArray.push(data[i]);
  } else {
    console.log(`${data[i]} is already pushed into array`);
  }
}

console.log('Final Array: ', duplicatePushArray)

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

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.