0

I have an array with below list of array as shown in image , I would like to remove the duplicates array "Qualif" and "Prod"

what i have

I want to be like that

what i want

I have tried with the following conditions

Scenario1 :

   this.Default_data_historique=Object.values(this.nouveau_data_historique.reduce((c,e)=>{
            if (!c[e.PTF_NAME]) c[e.PTF_NAME] =e; return c; },{}));

Scenario2 :

this.Default_data_historique= Array.from(new Set(this.nouveau_data_historique))

Scenario3 :

this.Default_data_historique=this.nouveau_data_historique.filter(function(elem,index,self){
          return index === self.indexOf(elem)
        });

None of the three scenarios above were able to remove the duplicates from that array. Could any one please help on this ?

Edit: sample data i have

[
    [{
            "PTF_NAME": "integration",
            "Salle": "salle1"
        },
        {
            "PTF_NAME": "integration",
            "Salle": "salle1"
        }
    ],
    [{
            "PTF_NAME": "Qualif",
            "Salle": "salle1"
        },
        {
            "PTF_NAME": "Qualif",
            "Salle": "salle1"
        }
    ],
    [{
            "PTF_NAME": "Qualif",
            "Salle": "salle2"
        },
        {
            "PTF_NAME": "Qualif",
            "Salle": "salle2"
        }
    ],
    [{
            "PTF_NAME": "Prod",
            "Salle": "salle1"
        },
        {
            "PTF_NAME": "Prod",
            "Salle": "salle1"
        }
    ],
    [{
            "PTF_NAME": "Prod",
            "Salle": "salle2"
        },
        {
            "PTF_NAME": "Prod",
            "Salle": "salle2"
        }
    ]
]

what i want

[
    [{
            "PTF_NAME": "integration",
            "Salle": "salle1"
        },
        {
            "PTF_NAME": "integration",
            "Salle": "salle1"
        }
    ],
    [{
            "PTF_NAME": "Qualif",
            "Salle": "salle1"
        },
        {
            "PTF_NAME": "Qualif",
            "Salle": "salle1"
        }
    ],

    [{
            "PTF_NAME": "Prod",
            "Salle": "salle1"
        },
        {
            "PTF_NAME": "Prod",
            "Salle": "salle1"
        }
    ]

]
3
  • put the actual json in the question. Commented Apr 21, 2020 at 18:55
  • @programoholic it's many file json in one data , the data is like the first image just i want to remove or reduce the second array for "Qualif" and "Prod" Commented Apr 21, 2020 at 18:59
  • @programoholic i put some arrays in link few arrays Commented Apr 21, 2020 at 19:27

2 Answers 2

1

Try this,

data = [
    [
        {
            "PTF_NAME": "integration",
            "Salle": "salle1"
        },
        {
            "PTF_NAME": "integration",
            "Salle": "salle1"
        }
    ],
    [
      {
            "PTF_NAME": "Qualif",
            "Salle": "salle1"
      },
      {
            "PTF_NAME": "Qualif",
            "Salle": "salle1"
      }
    ],
    [
      {
            "PTF_NAME": "Qualif",
            "Salle": "salle2"
      },
      {
            "PTF_NAME": "Qualif",
            "Salle": "salle2"
      }
    ],
    [{
            "PTF_NAME": "Prod",
            "Salle": "salle1"
        },
        {
            "PTF_NAME": "Prod",
            "Salle": "salle1"
        }
    ],
    [{
            "PTF_NAME": "Prod",
            "Salle": "salle2"
      },
        {
            "PTF_NAME": "Prod",
            "Salle": "salle2"
        }
    ]
];
function removeDupesInArrayOfObject() {
  const result = [];
  result.push(this.data[0]);
  data.reduce( (first, second, n) => {
      if (first[0].PTF_NAME !== second[0].PTF_NAME) {
          result.push(second);
      }
      return second
  })
  console.log(result);
}
removeDupesInArrayOfObject();

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

16 Comments

post a few arrays of data in the question
i don't know why but done , see the link of image few arrays
,i try the new answer but nope sorry same result of my first image
Maybe you don't understand my question , i want the result like the second image , i want to remove the second array of "Qualif" and "Prod" check again what i have and what i want.
i try the new answer and i have just one array and one item for "integration"
|
0

You can use filter() method.

this.nouveau_data_historique.filter((item,index) => {
  return this.nouveau_data_historique.indexOf(item) === index
 });

1 Comment

Thank you for reply, but sorry same thing , same first result

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.