-6

I want to find out the string element which is exist or not in array. In other words, I can explain what I want to search. In the below code if I pass "text" to a function then how to find out the index of each element which consist "text" in array. that's the array and I want to print the index of text elements which exists in elements array but the condition is we only need to pass text in arguments.

"steps": [
  {
    "stepname": "Step 1",
    "stepnum": 1,
    "elements": [
      "div-container-s1",
      "text-1",
      "text-2",
      "text-3",
      "image-1",
      "image-2",
      "button-1",
      "button-2",
      "image-3"
    ]
  },
  {
    "stepname": "Step 2",
    "stepnum": 2,
    "elements": [
      "div-container-s2",
      "text-4",
      "text-5",
      "text-6",
      "image-4",
      "image-5",
      "image-6",
      "form-1",
      "input-1",
      "button-3",
      "dropdown-1"
    ]
  },
  {
    "stepname": "Step 3",
    "stepnum": 3,
    "elements": [
      "div-container-s3",
      "text-7",
      "text-8",
      "text-9",
      "image-7",
      "text-10",
      "text-15",
      "text-16",
      "text-17",
      "text-18",
      "image-10",
      "image-11",
      "image-12",
      "image-13",
      "image-8",
      "image-9",
      "text-11",
      "text-12",
      "text-13",
      "text-14"
    ]
  }
],

function myFunction(elementType){
//code goes here
}

myFunction('text');

//output should be 0,1,2,3 because it contains "text" 
Dev
  • 57
  • 9
  • 3
    `//code goes here`? The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Jan 04 '19 at 06:18
  • Please specify what you have tried so far to solve this problem. – Noor A Shuvo Jan 04 '19 at 06:21
  • actually there is an object of 1600 lines, so I can not share it all – Dev Jan 04 '19 at 06:22
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes and a nice for loop. 1600 line input is rather irrelevant here, just post your function code, what you've tried to solve problem. – Drew Reese Jan 04 '19 at 06:24
  • Duplicate: https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript – Srikrushna Jan 04 '19 at 06:24
  • Look like already answered https://stackoverflow.com/questions/6116474/how-to-find-if-an-array-contains-a-specific-string-in-javascript-jquery – Srikrushna Jan 04 '19 at 06:25
  • how to solve this by using indexOf method, but but but we only need to pass "text" rather than "text-1" or something.. – Dev Jan 04 '19 at 06:30
  • Possible duplicate of [How do I check if an array includes an object in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) – Hydrothermal Jan 04 '19 at 06:31

3 Answers3

1

You can use reduce function on the array and use includes to check if the string contains the text.If it is so then push the index n the accumulator

var a = ["text-1", "text-2", "text-3", "text-4", "div-container-s1", "image-4"];

function myFunction(elementType) {
  return a.reduce((acc, curr, index) => {
    if (curr.includes(elementType)) {
      acc.push(index)
    }
    return acc;
  }, [])
}

console.log(myFunction('text'));
brk
  • 48,835
  • 10
  • 56
  • 78
0

try the below example

var a=["text-1","text-2","text-3","text-4","div-container-s1", "image-4"];

function getTextIndex(arrayElement) {
  var char='';
  for(var i=0; i<arrayElement.length; i++) {
    if(arrayElement[i].search(/[^a-zA-Z]+/) == -1) { 
    if(char == '')
      char = i.toString();
    else
      char = char + ', ' + i;
    }
  }
  console.log(char)
}

getTextIndex(a[0]);
0

You can use map and filter for that. First with map return index which consists of required string. If it doesn't have that string then return -1 as value. Then simply filter your array to remove all entries of -1.

var a = ["text-1","text-2","text-3","text-4","div-container-s1", "image-4"];

function myFunction(text) {
  return a.map((x, i) => x.includes('text') ? i : -1).filter(x => x != -1);
}

console.log(myFunction('text'));
Karan
  • 12,059
  • 3
  • 24
  • 40