skip to Main Content

I have an array of object,I need to get those objects only whose property "name" having similar value.I have provided the expected output also.Here is the code below.

 <!DOCTYPE html>
    <html>
    <body>
    <h2>JavaScript</h2>
    <script>
    const value = 

    [
        {
            "id": 2,
            "name": "Pumpkin"
        },
        {
            "id": 1,
            "name": "Ladies Finger"
        },
        {
            "id": 4,
            "name": "Spinach"
        },
        {
            "id": 5,
            "name": "Spinach"
        }
    ]
    console.log("get same value property")
   /*Expected output 
[
    {
        "id": 4,
        "name": "Spinach"
    },
    {
        "id": 5,
        "name": "Spinach"
    }
]
*/
</script>
</body>
</html> 

3

Answers


  1. const arr = [{
        "id": 2,
        "name": "Pumpkin"
      },
      {
        "id": 1,
        "name": "Ladies Finger"
      },
      {
        "id": 4,
        "name": "Spinach"
      },
      {
        "id": 5,
        "name": "Spinach"
      }
    ]
    
    const tmp = arr.reduce((acc, item) => {
      if (acc[item.name])
        acc[item.name].push(item)
      else
        acc[item.name] = [item]
    
      return acc
    }, {})
    
    const result = Object.values(tmp).filter(i => i.length > 1).flat()
    console.log(result)
    Login or Signup to reply.
  2. You can iterate over array once to create the key value pairs to store the occurence of the elements. And then use this key value pair to filter out the array elements using filter function. Something like this

    const dataKeyMaps = value.reduce((vl, e) => {
      vl[e.name] = ++vl[e.name] || 0;
      return vl;
    }, {});
    const duplicateObjects = value.filter(x => dataKeyMaps[x.name]);
    
    const value = [{
        "id": 2,
        "name": "Pumpkin"
      },
      {
        "id": 1,
        "name": "Ladies Finger"
      },
      {
        "id": 4,
        "name": "Spinach"
      },
      {
        "id": 5,
        "name": "Spinach"
      }
    ]
    
    const dataKeyMaps = value.reduce((vl, e) => {
      vl[e.name] = ++vl[e.name] || 0;
      return vl;
    }, {});
    const duplicateObjects = value.filter(x => dataKeyMaps[x.name]);
    console.log(duplicateObjects)
    Login or Signup to reply.
  3. Good question!
    https://jsfiddle.net/coder039/3fu87h6j/1/

    const propertyCounter = () => {
      const data = {}
      return {
        add(item) {
          if (data.hasOwnProperty(item)) {
            data[item] = data[item] + 1
          } else {
            data[item] = 1
          }
        },
        get(item) {
          return data[item]
        },
        getData() {
          return data
        },
      }
    }
    
    const arr = [{
        id: 2,
        name: "Pumpkin",
      },
      {
        id: 1,
        name: "Ladies Finger",
      },
      {
        id: 4,
        name: "Spinach",
      },
      {
        id: 5,
        name: "Spinach",
      },
    ]
    const counter = propertyCounter()
    arr.forEach((item) => {
      counter.add(item.name)
    })
    
    const result = arr.filter((obj) => counter.getData()[obj.name] >= 2)
    console.log(result)
    document.write(JSON.stringify(result))
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search