2

I want to check condition inside array with single object.

let arr = [
  {
    name: "john",
    description: {
      height: 5.5,
      weight: 54,
    },
  },
  {
    name: "mary",
    description: {
      height: 5.8,
      weight: 65,
    },
  },
  {
    name: "smith",
    description: {
      height: 6.1,
      weight: 85,
    },
  },
];

let obj = {
  height: 5.8,
  weight: 65,
};

i want to compare the obj within the array and if its match one, i want to get the name. for ex, obj is equal to marry. i want to print mary. This is what i have tried.

let result = arr.filter((item) => item.description === obj )
console.log(result.name);
8
  • 2
    and what is the problem? Commented May 25, 2020 at 19:57
  • If you want to find the one for mary, why not item.name === 'mary'? Commented May 25, 2020 at 19:57
  • Otherwise generalized object equality is only true if the objects are the exact same objects in memory. If they are not, item.description === obj will be false, even if the key value pairs of both objects match. Commented May 25, 2020 at 19:58
  • i know only the height and weight of the person. with that information i want to find the name. Commented May 25, 2020 at 19:58
  • item.description.height === obj.height && item.description.weight === obj.weight Commented May 25, 2020 at 19:59

5 Answers 5

2

You could build a filter with the entries of the object and iterate all entries and check the values. Then map the names.

let array = [{ name: "john", description: { height: 5.5, weight: 54 } }, { name: "mary", description: { height: 5.8, weight: 65 } }, { name: "smith", description: { height: 6.1, weight: 85 } }],
    object = { height: 5.8, weight: 65 },
    filters = Object.entries(object),
    result = array
        .filter(({ description }) => filters.every(([key, value]) => description[key] === value))
        .map(({ name }) => name);

console.log(result);

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

Comments

1

You have to compare the desired keys against each other, rather than comparing the objects, as object equality is based upon their memory addresses being the same.

let arr = [
  {
    name: "john",
    description: {
      height: 5.5,
      weight: 54
    }
  },
  {
    name: "mary",
    description: {
      height: 5.8,
      weight: 65
    }
  },
  {
    name: "smith",
    description: {
      height: 6.1,
      weight: 85
    }
  }
];

let obj = {
  height: 5.8,
  weight: 65
};

console.log(
  arr.filter(item => item.description.height === obj.height && item.description.weight === obj.weight)
);

Comments

1

you can narrow down the the property that you want to filter on like this

  let arr = [{
      name: "john",
      description: {
        height: 5.5,
        weight: 54
      }
    },
    {
      name: "mary",
      description: {
        height: 5.8,
        weight: 65
      }
    },
    {
      name: "smith",
      description: {
        height: 6.1,
        weight: 85
      }
    }
  ];

  let obj = {
    height: 5.8,
    weight: 65
  };

  const filtered = arr.filter(({ description }) => description.height === obj.height && description.weight === obj.weight);
  console.log(filtered)

Comments

1

One approach would be using Object.keys.

var arr = [ { name: "john", description: { height: 5.5, weight: 54, }, }, { name: "mary", description: { height: 5.8, weight: 65, }, }, { name: "smith", description: { height: 6.1, weight: 85, }}];

var obj = { height: 5.8, weight: 65 };

var result = arr.filter(({description})=>Object.keys(description).every(k=>description[k]==obj[k])).map(({name})=>name);

console.log(result);

Comments

0

The root cause for the failure of filter filter condition is due to the comparison between objects being attempted.

Comparison of objects can be done by using a helper function.

Here is the working exmaple:

// array-filter-demo.js
let arr = [
    { name: "john", description: { height: 5.5, weight: 54 } },
    { name: "mary", description: { height: 5.8, weight: 65 } },
    { name: "smith",description: { height: 6.1, weight: 85 } }
  ]

let obj = { height: 5.8, weight: 65 }

// Helper function to compare objects
function matching(a, b) {
   return ( a.height === b.height && a.weight === b.weight)
}

// Call the helper function inside filter
const result = arr.filter(item => (matching(item.description, obj)))
console.log(result)

Output:

$ node array-filter-demo.js 

[ { name: 'mary', description: { height: 5.8, weight: 65 } } ]

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.