Im trying to filter an array but im not arriving at the expected solution. Can someone please help. In this array i need to filter out if from.organization_details.type is 'SELLER_USER' and to.type value is 'OPERATOR'. Also if from.organization_details.type == 'OPERATOR_USER' and to[0].type value is 'SHOP'
[
{
"body": "Could you please elaborate on the discount",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
}
]
},
{
"body": "for testing purpose",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
{
"display_name": "Operator",
"type": "OPERATOR"
}
]
},
{
"body": "i need to know about warranty ",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
}
]
},
{
"body": "this is a test messgae from seller on dec 17 21",
"from": {
"display_name": "[email protected]",
"organization_details": {
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
"type": "SHOP_USER"
},
"to": [
{
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
}
]
},
{
"body": "hi there",
"from": {
"display_name": "Operator",
"organization_details": {
"display_name": "Operator",
"type": "OPERATOR"
},
"type": "OPERATOR_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
{
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
}
]
},
{
"body": "hello this is a test messsage on jan 5 2022",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": {
"display_name": "vandhana jayaprakash",
"id": "2211",
"type": "CUSTOMER"
},
"type": "CUSTOMER_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
{
"display_name": "Operator",
"type": "OPERATOR"
}
]
},
{
"body": "this is a message sent to operator ",
"from": {
"display_name": "[email protected]",
"organization_details": {
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
},
"type": "SHOP_USER"
},
"to": [
{
"display_name": "Operator",
"type": "OPERATOR"
}
]
},
{
"body": "this is a msg from operator to seller ",
"from": {
"display_name": "Operator",
"organization_details": {
"display_name": "Operator",
"type": "OPERATOR"
},
"type": "OPERATOR_USER"
},
"to": [
{
"display_name": "Taufiq Live Store",
"id": "2001",
"type": "SHOP"
}
]
}
]
I tried below code (Please see fiddle link) but its not working as expected. It eliminates the array entry if it contains to.type == 'CUSTOMER'. I want to eliminate only last two entries in my array.
Thanks in advance
https://jsfiddle.net/4m9kgfpv/
CodePudding user response:
Using Array#filter, iterate over the array and keep the element if:
from.organization_details.typeisCUSTOMER
OR
tohas an element wheretypeisCUSTOMER(useArray#some)
const conferenceDays = [
{
"body": "Could you please elaborate on the discount",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" },
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" }
]
},
{
"body": "for testing purpose",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER"},
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
{ "display_name": "Operator", "type": "OPERATOR" }
]
},
{
"body": "i need to know about warranty ",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" },
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" }
]
},
{
"body": "this is a test messgae from seller on dec 17 21",
"from": {
"display_name": "[email protected]",
"organization_details": { "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
"type": "SHOP_USER"
},
"to": [
{ "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" }
]
},
{
"body": "hi there",
"from": {
"display_name": "Operator",
"organization_details": { "display_name": "Operator", "type": "OPERATOR" },
"type": "OPERATOR_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
{ "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" }
]
},
{
"body": "hello this is a test messsage on jan 5 2022",
"from": {
"display_name": "vandhana jayaprakash",
"organization_details": { "display_name": "vandhana jayaprakash", "id": "2211", "type": "CUSTOMER" },
"type": "CUSTOMER_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
{ "display_name": "Operator", "type": "OPERATOR" }
]
},
{
"body": "this is a message sent to operator ",
"from": {
"display_name": "[email protected]",
"organization_details": { "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" },
"type": "SHOP_USER"
},
"to": [
{ "display_name": "Operator", "type": "OPERATOR" }
]
},
{
"body": "this is a msg from operator to seller ",
"from": {
"display_name": "Operator",
"organization_details": { "display_name": "Operator", "type": "OPERATOR" },
"type": "OPERATOR_USER"
},
"to": [
{ "display_name": "Taufiq Live Store", "id": "2001", "type": "SHOP" }
]
}
];
const arr = conferenceDays.filter(({ from = {}, to = [] }) =>
(from.organization_details.type === 'CUSTOMER') ||
(to.some(({ type }) => type === 'CUSTOMER'))
);
console.log(arr);
CodePudding user response:
there is function called filter for arrays that does exactly what you want, you can read about it here, but i write an example here aswell.
imagine we have and array and and we wanna filter all the values that are lower or equal to 2, we should create such a condition in array.filter callback that only values more than 2 return true. in your case you just need to check properties to match what you want, hope that helped.
let array = [1, 2, 3, 4, 5];
console.log(array.filter(element => {
return element > 2;
}));
CodePudding user response:
Here is your solution. it will work if you have only 1 element in your to array
const arr = [
{
body: "Could you please elaborate on the discount",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "SELLER_USER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "OPERATOR",
},
],
},
{
body: "for testing purpose",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
{
display_name: "Operator",
type: "OPERATOR",
},
],
},
{
body: "i need to know about warranty ",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
],
},
{
body: "this is a test messgae from seller on dec 17 21",
from: {
display_name: "[email protected]",
organization_details: {
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
type: "SHOP_USER",
},
to: [
{
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
],
},
{
body: "hi there",
from: {
display_name: "Operator",
organization_details: {
display_name: "Operator",
type: "OPERATOR",
},
type: "OPERATOR_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
{
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
],
},
{
body: "hello this is a test messsage on jan 5 2022",
from: {
display_name: "vandhana jayaprakash",
organization_details: {
display_name: "vandhana jayaprakash",
id: "2211",
type: "CUSTOMER",
},
type: "CUSTOMER_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
{
display_name: "Operator",
type: "OPERATOR",
},
],
},
{
body: "this is a message sent to operator ",
from: {
display_name: "[email protected]",
organization_details: {
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
type: "SHOP_USER",
},
to: [
{
display_name: "Operator",
type: "OPERATOR",
},
],
},
{
body: "this is a msg from operator to seller ",
from: {
display_name: "Operator",
organization_details: {
display_name: "Operator",
type: "OPERATOR",
},
type: "OPERATOR_USER",
},
to: [
{
display_name: "Taufiq Live Store",
id: "2001",
type: "SHOP",
},
],
},
];
// Previous array length
console.log(arr.length);
arr.forEach((value, i) => {
// If the condition is true than remove that value from the array
if (
(value.from.organization_details.type == "SELLER_USER" &&
value.to[0].type === "OPERATOR") ||
(value.from.organization_details.type == "OPERATOR_USER" && value.to[0].type === "SHOP")
) {
arr.splice(i, 1);
}
});
// Array length after removing data from array
console.log(arr.length);
