Javascript: Correct Syntax to find if string exists in Array

  1. In the DO…WHILE loop I need to extract 5000 items from a list (LookupList) and create a new list (arrayAll).
    I need help validating which of these lines is correct, or if not what is the correct syntax:
    arrayAll.push.apply(arrayAll, arrayBatch);
    arrayAll = arrayAll.concat(arrayBatch);

  2. Then I want to find if the string (LookupField) exists in the new list (arrayAll).
    Here I need help getting the correct syntax. The following I have tried do not work
    var listItem = $.inArray(LookupField, arrayAll) > -1;
    var listItem = arrayAll.where(item => item.fieldValues[“Title”].toString().contains(LookupField)).toList();
    var listItem = arrayAll.getItems(camlQuery_item);

Here is the code.

<input type='button' value='get Lists' onclick="PopulateLookupField();"/>
<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">

var LookupField = 'lteam@xyz.com';
var batchSize = 4000;

function PopulateLookupField() 
{
	var clientContext = new SP.ClientContext.get_current();
	var LookupList = clientContext.get_web().get_lists().getByTitle('test_list_dls');
	
	var camlQuery_item = new SP.CamlQuery();
	camlQuery_item.set_viewXml('<View><Query><Where><Eq> <FieldRef Name=\'Title\' /> <Value Type=\'Text\'>' + LookupField + '</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>');
	
	var camlQuery_list = new SP.CamlQuery();
	camlQuery_list.set_viewXml('<View Scope=\'RecursiveAll\'><Query><OrderBy><FieldRef Name=\'ID\' Ascending=\'TRUE\'/></OrderBy></Query><RowLimit Paged=\'TRUE\'>' + batchSize + '</RowLimit></View>');
	
	var arrayAll = new Array();
	do
	{
		arrayBatch = LookupList.getItems(camlQuery_list);
		clientContext.load(arrayBatch);
		clientContext.executeQuery;
		//arrayAll.push.apply(arrayAll, arrayBatch);
		arrayAll = arrayAll.concat(arrayBatch);
		camlQuery_list.listItemCollectionPosition = arrayBatch.listItemCollectionPosition;
	} while (camlQuery_list.listItemCollectionPosition != null);
	
	clientContext.load(arrayAll);
	clientContext.executeQueryAsync;	
	//var listItem = arrayAll.where(item => item.fieldValues["Title"].toString().contains(LookupField)).toList();
	//var listItem = arrayAll.getItems(camlQuery_item);
	var listItem = $.inArray(LookupField, arrayAll) > -1;

	alert("ItemFound = " + listItem);
}

</script>

You haven’t provided a lot of information as to what you’re doing – you’re using some really old SharePoint library to query something in SharePoint? And you’re using jQuery for some reason as well? And I assume you’ve just copied the code from somewhere? There’s a very weird mix of very old-looking JS syntax that a. looks like it was written 20 years ago and b. looks like C#. Anyway, this looks suspicious:

clientContext.executeQueryAsync;

That’s a function; you aren’t calling the function it so it doesn’t do anything

push mutates the array arrayAll by pushing a value (arrayBatch) onto it. So if arrayAll is [1,2,3] and arrayBatch is [1,2,3], then arrayAll.push(arrayBatch) would result in arrayAll being [1,2,3,[1,2,3]]. So you’ve used apply because it allows you to pass arguments as an array, so it’s the same as arrayAll.push(1,2,3) and you get [1,2,3,1,2,3].

You can just do arrayAll.push(...arrayBatch), you don’t need to use apply.

That will do almost the same thing but it won’t mutate, it’ll create a new array which you assign to the variable arrayAll.

Thanks for the comments Dan. Here are additional details:
I have a SharePoint list which has over 24000 lines. Hence I can cannot search the list directly, because SharePoint has a limitation of 5000. Hence I am using a CAML query with a DO…While loop to extract like 5000 items at a time and keep adding them to the array (arrayAll ).
So I would assume I need arrayAll.push(arrayAll, arrayBatch); correct?

Ok. Now for the next part. I need to search for the existence of the string ‘lteam@xyz.com’ in the new array (arrayAll) that we just created and return a true or false. I have tried three different “var listItem =” commands (as mentioned above) but all return false, even though (I have confirmed that) the list contains the search line.
Thanks

I assume you do not want nested arrays?

You can use includes if you just want true/false to be returned.

I am not understanding where the includes would go.
Here are the three lines of code I have tried as mentioned above:
var listItem = $.inArray(LookupField, arrayAll) > -1;
var listItem = arrayAll.where(item => item.fieldValues[“Title”].toString().contains(LookupField)).toList();
var listItem = arrayAll.getItems(camlQuery_item);

const arrayAll = ['test', 'test2', 'lteam@xyz.com', 'test3'];
console.log(arrayAll.includes('lteam@xyz.com')); // true

I’m not sure how much I can really comment on your actual code because it’s kind of confusing to read. But that is the basic usage of includes anyway.

Ok Thanks. I ran the code with the new suggestions:
If I use arrayAll.push(...arrayBatch); I get an error “Found non-callable @@iterator
So I ended up using arrayAll = arrayAll.concat(arrayBatch);
With this concat line, the DO…While loop goes through fine.
Although it fails on the clientContext.executeQueryAsync; which would mean that it actually failed to load arrayAll correctly (in the previous line).
TypeError: a.get_$1B_0 is not a function

What are you using to run code here? Is it a very old browser (ie are you trying to do this in Internet Explorer)?

Yes, well that won’t do anything. However apart from that it’s not clear which particular part of your use of the SharePoint library functionality is wrong. The thing that’s failing is some internal function in the minified version of the library you’re pulling into your app.

You’re doing a very specific thing using a specific, very old Microsoft library that is for a specific Microsoft product and you seem to have copy pasted some very old code from somewhere.

Make it do the simplest thing first. Can you access the context object? Then maybe, can you load one entry? Can you query one thing? Etc.

why are you assuming that its IE on an old browser. Its just Google chrome. Please see read the initial question again . Everything is already there. Also it is the simplest thing. If you cannot understand, please give someone else the opportunity to answer.

Please don’t be insulting. Two very experienced professionals are donating their time for free, trying to help fix your code. Please be respectful of their efforts.

I’m not assuming I’m asking.

This is not a criticism: you seem to have low level of basic understanding of the language – for example what the difference between mutating an array (push) and constructing an entirely new array (concat) is and what they are doing. You have something that needs to be a function call.

So it is not “the simplest thing” because it is a extremely difficult to ascertain your level of skill: I have to work on the assumption it is not high, because you are using what looks like copy paste code. And you are showing a bunch of very specific, complex code without explaining at all what it is. It is extremely unlikely that many people on the forum will have used the library you’re asking for help with and it is therefore very important for you to explain what it does.

It is erroring on an internal function in the library. The library code you are using is minified, meaning that the error message is basically garbage (beyond the error possibly triggering on this line)

You also need to actually call the function that I told you needs to be a function call: this is non-negotiable, the query can’t be made if you don’t make the query.

Overall I am trying to get you to do the simplest thing first, make sure it works, do the next thing. Check that works. Etc. This is much easier to debug than “I have this large complex piece of code, it’s broke, fix it”

1 Like

Never mind, I will try elsewhere.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.