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.

9
  • 1
    what do you mean with "different levels of the object"? Commented May 3, 2019 at 7:10
  • Possible duplicate of How to find duplicate values in a JavaScript array of objects, and output only unique values? Commented May 3, 2019 at 7: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. Commented May 3, 2019 at 7:12
  • @NinaScholz I tried to explain comparing getResponse["taskData"][i][1] and constructing an array including the results as stated. Commented May 3, 2019 at 7: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. Commented May 3, 2019 at 7:15

3 Answers 3

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

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

Comments

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

3 Comments

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?
the reduce function changes (only) the accumulator c, which starts with an really empty object (no prototypes) and this is returned as result.
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.
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)

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.