简体   繁体   English

如果一个属性等于另一个 object 的属性,如何过滤位于对象数组中的 object 的少数属性

[英]How to filter few properties of an object which is in Array of objects if one property equals property from another object

I have a object which has some properties for one user, and I have array of objects which is returned from API.我有一个 object,它具有一个用户的一些属性,并且我有从 API 返回的对象数组。 My goal is to check which object of Array of objects has the same property as the one single initial object, and then it should return only part of it's properities.我的目标是检查对象数组中的哪个 object 与单个初始 object 具有相同的属性,然后它应该只返回部分属性。 I have tried to use.map on Array of objects but it seems not workig.我曾尝试在对象数组上使用.map,但它似乎不起作用。

Below is the code example.下面是代码示例。 I have also prepared codesandbox if You wish.如果您愿意,我还准备了代码沙盒。

const user = 
    {
      name: "jan",
      lastName: "kowalski",
      fullName: "jan kowalski",
      car: "audi"
    }
  ;

  const usersAnimal = [
    {
      name: "jan",
      lastName: "kowalski",
      fullName: "jan kowalski",
      animal: "cat",
      animalSize: "small",
      animalName: "Bat"
    },
    {
      name: "john",
      lastName: "smith",
      fullName: "john smith",
      animal: "dog",
      animalSize: "middle",
      animalName: "Jerry"
    },
    {
      name: "Anna",
      lastName: "Nilsson",
      fullName: "Anna Nilsson",
      animal: "cow",
      animalSize: "big",
      animalName: "Dorrie"
    }
  ];

  const filtered = usersAnimal.map((userAnimal)=>userAnimal.fullName === user.fullName && return userAnimal.animalName & userAnimal.animalSize & userAnimal.animal);

thanks谢谢

https://codesandbox.io/s/admiring-edison-qxff42?file=/src/App.js https://codesandbox.io/s/admiring-edison-qxff42?file=/src/App.js

For case like this, it would be far easier if you filter it out first then proceed using map:对于这种情况,如果您先将其过滤掉,然后使用 map 继续操作,会容易得多:

 const filtered = usersAnimal
    .filter((animal) => animal.fullName === user.fullName)
    .map(({ animalName, animalSize, animal }) => {
      return {
        animalName,
        animalSize,
        animal
      };
    });

If you want to filter, I recommend you to use filter .如果要过滤,我建议您使用filter

The map method will create a new array, the content of which is the set of results returned by each element of the original array after the callback function is operated map方法会创建一个新数组,其内容是原数组每个元素在回调function操作后返回的结果集合

 const user = {name:"jan",lastName:"kowalski",fullName:"jan kowalski",car:"audi"}; const usersAnimal = [{name:"jan",lastName:"kowalski",fullName:"jan kowalski",animal:"cat",animalSize:"small",animalName:"Bat"},{name:"john",lastName:"smith",fullName:"john smith",animal:"dog",animalSize:"middle",animalName:"Jerry"}]; // Get an array of matching objects let filtered = usersAnimal.filter(o => o.fullName === user.fullName); // You get the filtered array, then you can get the required properties filtered.forEach(o => { console.log( 'animal:%s, animalSize:%s, animalName:%s', o?.animal, o?.animalSize, o?.animalName ); }); // Then use map to process each element filtered = filtered.map(o => { const {animal, animalSize, animalName} = o; return {animal, animalSize, animalName}; }); console.log('filtered', filtered);

I am providing a for loop solution as I haven't learnt many array methods in javascript.我提供了一个 for 循环解决方案,因为我还没有在 javascript 中学习很多数组方法。

For me the simplest option is to use a for loop and an if check to loop through the arrays values to check for included values.对我来说,最简单的选择是使用 for 循环和 if 检查循环遍历 arrays 值以检查包含的值。

for (let v in usersAnimal) {
    if (usersAnimal[v].fullName === user.fullName) {
        console.log(usersAnimal[v])
    }
}

The code above will log the entire usersAnimal object containing the fullname we are looking for.上面的代码将记录整个 usersAnimal object 包含我们正在寻找的全名。

{
  name: 'jan',
  lastName: 'kowalski',
  fullName: 'jan kowalski',
  animal: 'cat',
  animalSize: 'small',
  animalName: 'Bat'
}

commented for further understanding评论以进一步理解

for (let v in usersAnimal) {
//loops though the array
    if (usersAnimal[v].fullName === user.fullName) {
    //when the index value 'v' has a fullname that matches the user fullname value
    // it passes the if check and logs that object value
        return console.log(usersAnimal[v])
    //return true...
    }
  //return null
}

//etc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JS-过滤对象数组以查找一个属性的重复项,并根据另一个属性确定要保留的对象 - JS - Filter an array of objects for duplicates of one property, and decide which object to keep based on another property 对象数组,用于累积另一个属性相同的所有对象属性 - Array of objects to accumulate all object properties for which another property is the same 如何将一个数组复制到另一个数组,其中一个属性是 object - How to copy an array to another array with one property which is an object 如何在VueJS中按对象属性过滤对象数组 - How to filter array of objects by object property in VueJS 如何根据公共属性的值从另一个对象数组的所有元素中过滤一个对象数组 - How to filter an arrayof objects from all elements of another array of object on values of a common property 根据另一个属性的值将属性从一个对象传输到另一个对象 - Transferring Properties from one to another object based on the value of another property 如何从属性等于Null的数组中删除对象-Lodash - How to Remove Object From Array Where Property Equals Null - Lodash Javascript:将一个数组分配给另一个数组,这是对象的属性 - Javascript : Assigning one array to another array which is a property of an object 如何根据对象数组中属性的第一个字符过滤对象数组? - How to filter an array of objects based on the first character of a property in the array of object? 如何过滤对象数组并检查数组内是否有多个 object 在 Javascript 中具有相同的属性值? - How to filter array of objects and check if more than one object inside the array has the same property value in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM