मैं एक सरणी फ़िल्टर करने की कोशिश कर रहा हूं लेकिन अपेक्षित समाधान पर नहीं पहुंच रहा हूं। क्या कोई कृपया मदद कर सकता है। इस सरणी में मुझे फ़िल्टर करना होगा यदि from.organization_details.type 'SELLER_USER' है और to.type मान 'OPERATOR' है। साथ ही अगर from.organization_details.type == 'OPERATOR_USER' और to[0].type value '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": "taufiqpainter@gmail.com",
"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": "taufiqpainter@gmail.com",
"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"
}
]
}
]
https://jsfiddle.net/4m9kgfpv/
3 जवाब
Array#filter
का इस्तेमाल करना , सरणी पर पुनरावृति करें और तत्व रखें यदि:
from.organization_details.type
CUSTOMER
है
या
to
में एक तत्व है जहांtype
CUSTOMER
है (Array#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": "taufiqpainter@gmail.com",
"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": "taufiqpainter@gmail.com",
"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);
सरणियों के लिए फ़िल्टर नामक फ़ंक्शन है जो ठीक वही करता है जो आप चाहते हैं, आप इसके बारे में पढ़ सकते हैं यहां, लेकिन मैं यहां एक उदाहरण भी लिखता हूं।
कल्पना कीजिए कि हमारे पास और सरणी है और हम उन सभी मानों को फ़िल्टर करना चाहते हैं जो 2 से कम या बराबर हैं, हमें array.filter कॉलबैक में ऐसी स्थिति बनानी चाहिए जो केवल 2 से अधिक मान सही लौटाए। आपके मामले में आपको अपनी इच्छित चीज़ों से मेल खाने के लिए केवल संपत्तियों की जांच करने की आवश्यकता है, आशा है कि इससे मदद मिली।
let array = [1, 2, 3, 4, 5];
console.log(array.filter(element => {
return element > 2;
}));
यहाँ आपका समाधान है। यह काम करेगा यदि आपके से सरणी में केवल 1 तत्व है
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: "taufiqpainter@gmail.com",
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: "taufiqpainter@gmail.com",
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);
to
एक सरणी है, कोईto.type
नहीं है। क्या आप और भी वर्णन कर सकते हैं कि क्या फ़िल्टर किया जाना चाहिएto
के लिए, आप केवल पहले तत्व की जांच करना चाहते हैं या यदि कोई तत्व मेल खाता है? तो आप रिकॉर्ड रखना चाहते हैं जहांfrom.organization_details.type
CUSTOMER
है या जहांto
में एक तत्व है जहांtype
CUSTOMER
है?