0

I have the below object

members
    {
        [
            age:30,
            list: [
                "PRICE",
                "LIST",
                "COUNTRY"
            ]
        ]
    },
    {
        [
            age:31,
            list: [
                "PRICE"
            ]
        ]
    },
    {
        [
            age:31,
            list: [
                "LIST"
            ]
        ]
    }

I need to check if array values are equal to specific value.

I need to check if list has PRICE or list has COUNTRY or list has PRICE,LIST,COUNTRY combination.

Currently I'm using includes which checks if value is present. But i need to check exact value

Array.isArray(members.list.map(message, index){
        if(message.includes("LIST"))
        {

        }
        if(message.includes("PRICE"))
        {

        }
         if(message.includes("PRICE") && message.includes("LIST") && message.includes("COUNTRY"))
        {
            //message.includes("PRICE") and ("LIST") is already executed, so this will execute again. But i need to execute the complete condition combination.
        }
    })

How to acheive this?

6
  • 2
    Array.isArray(members.list.map(...)) makes not that much sense as .map() will always return an array O.o Commented May 3, 2018 at 9:32
  • Check this post, it can help link Commented May 3, 2018 at 9:35
  • @Andreas so what should be the solution? Commented May 3, 2018 at 9:35
  • Turn the comparison to have the most specific first, then use else: if(message.includes("PRICE") && message.includes("LIST") && message.includes("COUNTRY")) {} else if(message.includes("LIST")) {} else if(message.includes("PRICE")) { } Commented May 3, 2018 at 9:35
  • @siraxtas but that post does not show about checking combination Commented May 3, 2018 at 9:37

3 Answers 3

0

Your concern is to loop through all possible if condition.
So you shouldn't do return within a condition checking.
You can save the result and override it whenever it matches a condition. Hope can help.
Also there is some problem of your origin members Object. Also fixed in the following demo.

let members = [
	{ age: 30, list: ["PRICE", "LIST", "COUNTRY"] },
	{ age: 31, list: ["PRICE"] },
	{ age: 31, list: ["LIST"] }
];

   Array.prototype.inArray = function () {
var message = this;
if (message.includes("PRICE") && message.includes("LIST") && message.includes("COUNTRY")) {
	return "contains PRICE, LIST and COUNTRY";
}
if (message.includes("PRICE") && message.includes("LIST") ) {
	return "contains PRICE and LIST ";
}
if (message.includes("PRICE") &&  message.includes("COUNTRY")) {
	return "contains PRICE and COUNTRY";
}
if (message.includes("LIST") && message.includes("COUNTRY")) {
	return "contains LIST and COUNTRY";
}
if (message.includes("LIST")) {
	return "contains LIST";
}
if (message.includes("PRICE")) {
	return "contains PRICE";
}
if (message.includes("LIST")) {
	return "contains LIST";
}
}

for (let member of members) {
	console.log(member.list.inArray());
}

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

2 Comments

I want either PRICE or PRICE, LIST, COUNTRY combination to execute.. In your code, both will execute
Sorry that i have misunderstand your question. The most stupid solution are as the edited answer.
0

Hope this will help

var members = [
	{ age: 30, list: ["PRICE", "LIST", "COUNTRY"] },
	{ age: 31, list: ["PRICE"] },
	{ age: 31, list: ["LIST"] }
];
members.forEach((val,key)=>{
	if(Array.isArray(val.list)){
		if(val.list.indexOf('PRICE') > -1 || val.list.indexOf('COUNTRY') > -1 || (val.list.indexOf('PRICE') > -1 && val.list.indexOf('COUNTRY') > -1 && val.list.indexOf('LIST') > -1)){
			console.log(val)
		}
	}
});

Comments

0

You can compose a filter function and use that for Array.prototype.filter

const members = [
  {
    age: 30,
    list: ["PRICE","LIST","COUNTRY"]
  },
  {
    age: 31,
    list: ["PRICE"]
  },
  {
    age: 31,
    list: ["LIST"]
  },
  {
    age: 88,
    list: ["not this one"]
  },
  {
    age: 88,
    list: ["not this one","COUNTRY","NOTPRICEORLIST"]
  }
]

const getList = o => (o&&o.list) || [];//get list member from o or returns empty array
const contains = needle => haystack => haystack.includes(needle);//check if haystack contains needle
const containsAll = needles => haystack => needles.reduce(
  (result,needle)=>result && haystack.includes(needle),
  true
);
const countryOrPriceOrPriceListCountry = haystack =>
  contains("PRICE")(haystack) || contains("LIST")(haystack)
  //this is pointless, would already be true because it contains price or country
  // || containsAll(["PRICE","LIST","COUNTRY"])(haystack);

const filter = getter => comparer => item =>//get something from item (list) and send to compare function
  comparer(getter(item));

console.log(
  members.filter(filter(getList)(countryOrPriceOrPriceListCountry))
);

Or maybe you were looking for the following:

const members = [
  {
    age: 30,
    list: ["PRICE","LIST","COUNTRY"]
  },
  {
    age: 31,
    list: ["PRICE"]
  },
  {
    age: 32,
    list: ["LIST"]
  },
  {
    age: 88,
    list: ["not this one"]
  },
  {
    age: 89,
    list: ["not this one","COUNTRY","NOTPRICEORLIST"]
  }
]

const getList = o => (o&&o.list) || [];//get list member from o or returns empty array
const contains = needle => haystack => haystack.includes(needle);//check if haystack contains needle
const containsAll = needles => haystack => needles.reduce(
  (result,needle)=>result && haystack.includes(needle),
  true
);
const countryOrPrice = haystack =>
  contains("PRICE")(haystack) || contains("LIST")(haystack)
const countryListAndPrice = containsAll(["PRICE","LIST","COUNTRY"]);

members.map(
  item=>[item,countryOrPrice(getList(item))]//see if item has country or price
).map(
  ([item,hasCountryOrPrice])=>[
    item,
    hasCountryOrPrice,
    // will check for country list and price only if has country or price 
    hasCountryOrPrice && countryListAndPrice(getList(item))
  ]
).forEach(
  ([item,hasCountryOrPrice,countryListAndPrice])=>
    console.log(
      "item age:",
      item.age,
      "has country or price:",
      hasCountryOrPrice,
      "has country list and price:",
      countryListAndPrice
    )
);

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.