1

I got an array of object

    const devices = [{
    deviceLocation: {
      label: "Pure",
      value: "pure"
    },
    deviceName: "test7"
    devicePaymentMethods: [{
      label: "ab",
      value: "ab"
    } {
      label: "cd",
      value: "cd"
    } {
      label: "ef",
      value: "ef"
    }]
    deviceType: "iPad"
    id: "001"
    connected: false
    enabled: false
  },

  {
    deviceLocation: {
      label: "kandy",
      value: "kandy"
    },
    deviceName: "test4"
    devicePaymentMethods: [{
      label: "ab",
      value: "ab"
    } {
      label: "cd",
      value: "cd"
    }]
    deviceType: "iPad"
    id: "004"
    connected: false
    enabled: false
  }
]

I want to filter the array by object using deviceName,deviceLocation and devicePaymentMethods

Here is what I have already done and it is working for deviceName and deviceLocation but doesn't work for devicePaymentMethods

const filterByValue = (array, string) => {
return array.filter(o =>
  Object.keys(o).some(k => {
    if (k === "deviceName") {
      return o[k].toLowerCase().includes(string.toLowerCase());
    } else if (k === "deviceLocation") {
      return o[k].label.toLowerCase().includes(string.toLowerCase());
    } else if (k === "devicePaymentMethods") {
       o[k].map(pay => {
          if(pay.label.toLowerCase().includes(string.toLowerCase()))
        return pay;
      });
    }
  })
);
};

 filterByValue(devices, "ab")
2
  • 3
    please add the wanted result. Commented Feb 28, 2020 at 9:11
  • use filter instead of map Commented Feb 28, 2020 at 9:12

2 Answers 2

2

You forget to mentioned return while filtering the data inside map

return o[k].map(pay => {
  if(pay.label.toLowerCase().includes(string.toLowerCase()))
    return pay;

const devices = [{
    deviceLocation: {
      label: "Pure",
      value: "pure"
    },
    deviceName: "test7",
    devicePaymentMethods: [{
      label: "ab",
      value: "ab"
    }, {
      label: "cd",
      value: "cd"
    }, {
      label: "ef",
      value: "ef"
    }],
    deviceType: "iPad",
    id: "001",
    connected: false,
    enabled: false,
  },
  {
    deviceLocation: {
      label: "kandy",
      value: "kandy"
    },
    deviceName: "test4",
    devicePaymentMethods: [{
      label: "ab",
      value: "ab"
    }, {
      label: "cd",
      value: "cd"
    }],
    deviceType: "iPad",
    id: "004",
    connected: false,
    enabled: false,
  }
];


const filterByValue = (array, string) => {
  return array.filter(o =>
    Object.keys(o).some(k => {
      if (k === "deviceName") {
        return o[k].toLowerCase().includes(string.toLowerCase());
      } else if (k === "deviceLocation") {
        return o[k].label.toLowerCase().includes(string.toLowerCase());
      } else if (k === "devicePaymentMethods") {
        return o[k].some(pay => {
          if (pay.label.toLowerCase().includes(string.toLowerCase())) {
            return pay;
          }
        });
      }
    })
  );
};

console.log(filterByValue(devices, "ab"));
console.log(filterByValue(devices, "ef"));

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

2 Comments

filterByValue(devices, "ef") it will return same out put
Fixed the issue and used some() to retrieve one or more values. Hope this helps! please upvote the answer.
0

You could take a slightly different approach with an array of wanted keys and keys for nested array for search.

const
    keys = [['deviceName'], ['deviceLocation', 'label'], ['devicePaymentMethods', 'label']],
    filterByValue = (array, string) => array.filter(object => keys.some(([key, sub]) => {
        if (!(key in object)) return false;
        if (typeof object[key] === 'string' && object[key].toLowerCase() === string) return true;
        if (Array.isArray(object[key]) && sub)
            return object[key].some(o => o[sub].toLowerCase() === string);
    })),
    devices = [{ deviceLocation: { label: "Pure", value: "pure" }, deviceName: "test7", devicePaymentMethods: [{ label: "ab", value: "ab" }, { label: "cd", value: "cd" }, { label: "ef", value: "ef" }], deviceType: "iPad", id: "001", connected: false, enabled: false }, { deviceLocation: { label: "kandy", value: "kandy" }, deviceName: "test4", devicePaymentMethods: [{ label: "ab", value: "ab" }, { label: "cd", value: "cd" }], deviceType: "iPad", id: "004", connected: false, enabled: false }],
    result = filterByValue(devices, "ab");

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.