1

I have a JSON object (stringified) getResponse["taskData"] like this as an example:

[
   {title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0"},
   {title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0"},
   {title: "qwerty", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0"},
   {title: "asdfgh", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0"},
   {title: "zxcvbn", startOn: "2-5-2019-9-0", endOn: "2-5-2019-11-0"}
]

What I need is to know how many duplicates of startOn values on different levels of the multidimensional object.

For instance, getting 30-4-2019-18-0 (2), 1-5-2019-16-30 (2) as a result would be useful for me. Or, getting index integers of corresponding duplicates would also be helpful.

I've been trying to achieve such function using this:

var countSameStart = {};
getResponse["taskData"].forEach(function (x) {
    countSameStart[x] = ( countSameStart[x] || 0 ) + 1;
});

But, then I figured out that this function was unable to access different levels of the object and compare those values. Moreover, I couldn't wrap my mind around it.

I appreciate any constructive answers as code or recommendation.

sheptang
  • 82
  • 2
  • 12
  • 1
    what do you mean with *"different levels of the object"*? – Nina Scholz May 03 '19 at 07:10
  • Possible duplicate of [How to find duplicate values in a JavaScript array of objects, and output only unique values?](https://stackoverflow.com/questions/39885893/how-to-find-duplicate-values-in-a-javascript-array-of-objects-and-output-only-u) – Radonirina Maminiaina May 03 '19 at 07:11
  • What you've shown is an array of objects whose values are all strings. If you write `console.log(x)` inside the forEach you can see what you're dealing with. – jonrsharpe May 03 '19 at 07:12
  • @NinaScholz I tried to explain comparing getResponse["taskData"][i][1] and constructing an array including the results as stated. – sheptang May 03 '19 at 07:13
  • it would be easier, if you add a stringified (JSON) object (instead of a console copy) to the question. it does not have all rows or properties, but we need to get a sense of the object, you are talking about. – Nina Scholz May 03 '19 at 07:15
  • @NinaScholz I've updated that part, thanks. – sheptang May 03 '19 at 07:24
  • @RadonirinaMaminiaina I've checked your suggestion, but it didn't have the answer I was looking for. The similarities I saw were it was about duplicate detection, but simpler. – sheptang May 03 '19 at 07:27
  • actually i see no nested objects or arrays, just a single array of objects. what does exactly not work with your code? – Nina Scholz May 03 '19 at 07:27
  • @NinaScholz The problem was that the getResponse array has other versions of this object group, too. I needed to find a way without dividing the array, instead creating a new array of results so that I would be able to also push another set of results from the other part of the array. – sheptang May 03 '19 at 07:38

3 Answers3

2

You can create a function which takes property i.e startOn and value. Use forEach on array if prop matches the given value then push its index to result object.

const arr = [{title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0"}, {title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0"}, {title: "qwerty", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0"}, {title: "asdfgh", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0"}, {title: "zxcvbn", startOn: "2-5-2019-9-0", endOn: "2-5-2019-11-0"}]
   
function dups(arr,key,val){
  let res = [];
  arr.forEach((x,i) => {
    if(x[key] === val) res.push(i);
  })
  return res;
}

console.log(dups(arr,'startOn','30-4-2019-18-0'))
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
1

You can use array reduce and create an object where keys will be startOn & its value will be an array of objects. Then you can take that key value and use length to get the number of items with same startOn

let data = [{
    title: "123456",
    startOn: "30-4-2019-18-0",
    endOn: "30-4-2019-19-0"
  },
  {
    title: "123456",
    startOn: "30-4-2019-18-0",
    endOn: "30-4-2019-19-0"
  },
  {
    title: "qwerty",
    startOn: "1-5-2019-16-30",
    endOn: "1-5-2019-19-0"
  },
  {
    title: "asdfgh",
    startOn: "1-5-2019-16-30",
    endOn: "1-5-2019-19-0"
  },
  {
    title: "zxcvbn",
    startOn: "2-5-2019-9-0",
    endOn: "2-5-2019-11-0"
  }
]

let k = data.reduce(function(acc, curr) {
  if (acc[curr.startOn]) {
    acc[curr.startOn].push(curr)
  } else {
    acc[curr.startOn] = [curr]
  }
  return acc;

}, {});
console.log(k["30-4-2019-18-0"].length)
brk
  • 48,835
  • 10
  • 56
  • 78
1

Assuming, you have an object with arrays, then you could get the values and iterate the arrays and get the count of the wanted key.

var getResponse = { taskData: [{ title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0" }, { title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0" }, { title: "qwerty", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0" }, { title: "asdfgh", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0" }, { title: "zxcvbn", startOn: "2-5-2019-9-0", endOn: "2-5-2019-11-0" }], otherData: [{ title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0" }, { title: "qwerty", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0" }, { title: "asdfgh", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0" }] },
    key = 'startOn',
    count = Object.values(getResponse).reduce(
        (c, a) => (a.forEach(({ [key]: item }) => c[item] = (c[item] || 0) + 1), c),
        Object.create(null)
    );

console.log(count);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Please excuse my lack of knowledge, but I wonder if I use this snippet, would reduce function change the Object, or just assign the changed version to a variable? – sheptang May 03 '19 at 07:43
  • the reduce function changes (only) the accumulator `c`, which starts with an really empty object (no prototypes) and this is returned as result. – Nina Scholz May 03 '19 at 07:45
  • Okay, I needed the corresponding key, that's why I changed this to accepted answer. I can also use this with the other layers of the object, thanks for including it as an example, too. – sheptang May 03 '19 at 07:50