1

I want to browse an array to check if the elements are repeated and then recover the last element .

const tabs = [
  { name: 'john', details:"john1" },
  { name: 'michael' , details:"michael1" },
  { name: 'beris' , details:"beris1" },
  { name: 'john'  , details:"john2"},
  { name: 'john'  , details:"john3"},
  { name: 'beris' , details:"beris2" },
   { name: 'beris' , details:"beris3" },
]

I would like to have as result

const tabs = [
   { name: 'john'  , details:"john3"},
    { name: 'beris' , details:"beris3" },
]
3
  • What have you tried so far? Hint: a map/dictionary would be helpful. Commented Jun 17, 2020 at 9:19
  • Why isn't michael in the result? because it's not repeated? Commented Jun 17, 2020 at 9:19
  • @Kaddath yes michael does not repeat Commented Jun 17, 2020 at 9:40

4 Answers 4

1

const tabs = [
  { name: 'john', details:"john1" },
  { name: 'michael' , details:"michael1" },
  { name: 'beris' , details:"beris1" },
  { name: 'john'  , details:"john2"},
  { name: 'john'  , details:"john3"},
  { name: 'beris' , details:"beris2" },
  { name: 'beris' , details:"beris3" },
];

// logic to create newTabs array having only latest duplicates

let names=[];
let newTabs=[];

tabs.forEach((element)=>{
	if(names.includes(element.name)){
	newTabs = newTabs.filter(function( obj ){
		return obj.name !== element.name;
		});
	newTabs.push(element);
  }else{
	names.push(element.name);
  }
});

// output newTabs array that has only latest duplicate objects!
console.log(newTabs);

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

Comments

1

You can group objects with the same name into an array using a Map, where the key of the array is the name of the objects it stores. Then you can use Array.from() to convert the values from the map into an array, which you can then filter to only contain arrays where the occurrence is greater than one. For each array, you can then .map() it to get the last element:

const tabs = [{ name: 'john', details:"john1" }, { name: 'michael' , details:"michael1" }, { name: 'beris' , details:"beris1" }, { name: 'john'  , details:"john2"}, { name: 'john'  , details:"john3"}, { name: 'beris' , details:"beris2" }, { name: 'beris' , details:"beris3" },];

const res = Array.from(tabs.reduce((m, o) => 
  m.set(o.name, (m.get(o.name)|| []).concat(o))
, new Map).values()).filter(({length}) => length > 1).map(vals => vals.pop());
console.log(res);

Comments

1

You can group your items by name and take the last element of the group if it's longer than 2.

const lkp = tabs.reduce((acc, cur) => {
  acc[cur.name] = acc[cur.name] || [];
  acc[cur.name].push(cur); 
  return acc;
}, {});
const filtered = Object.values(lkp).reduce((acc, cur) => {
    if (cur.length > 1) acc.push(cur.pop());
    return acc;
}, [])
console.log (filtered)
<script>
const tabs = [
  { name: 'john', details:"john1" },
  { name: 'michael' , details:"michael1" },
  { name: 'beris' , details:"beris1" },
  { name: 'john'  , details:"john2"},
  { name: 'john'  , details:"john3"},
  { name: 'beris' , details:"beris2" },
   { name: 'beris' , details:"beris3" },
]
</script>

1 Comment

it also works for the method but I have not yet read the reduce () method, thank you too.
0

you can try the following

var map = {}
tabs.forEach(item => map[item.name] = item)
const result = Object.values(map)

1 Comment

it returns an element that does not repeat (michael).

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.