Array.split()

Hi everyone !!
I am having a doubt about this →

First of the code…

let value2 = [];
let value = [];

wixData.query("userCreations")
        .ne("dope", "")
        .find()
        .then((results) => {
 if (results.items.length > 0) {
 let items = results.items;
 for (var i = 0; i < results.items.length; i++) {
   value2  = items[i].dope;
     value[i] = value2.split(" ")[0];
 
     }
          console.log(value);

 

            } 
        });

While searching I found this bit of code->

var str = "How are you doing today?";
var res = str.split(" ")[0];

The result will be - “How”
The code will get the value before the space…

So…what I am trying to do is →

  1. Get the result (after query) as an array
  2. Get the first word of each array

Thanks!!

wixData.query("userCreations")
.ne("someField", "")
.isNotEmpty("someField")
.find()
.then(r => {
if(r.items.length > 0){
const firstWrods = r.items.map(e => e.someField.split(" ")[0]);
}
})

Hi J.D
Thanks !
I tried your code but it is returning an error

Console →

Code →

wixData.query("userCreations")
        .isNotEmpty("dope")
        .find()
        .then((results) => {
 if (results.items.length > 0) {
 let items = results.items;

 const firstWrods = results.items.map(e => e.hasTags.split(" ")[0]);
 
 
          console.log("first words  " + firstWrods)

            }
        });

@ajithkrr you’re trying to split the text of the hasTags field. How ever this field doesn’t contain any text value for some of the records. And it cannot split an undefined value. So add .isNotEmpty(“hasTags”) to your query.

@jonatandor35 Thanks !
Your code worked!!!
Actually, the field’s id was “hashTags”

Here’s the final code →

wixData.query("userCreations")
        .isNotEmpty("hashTags")
        .find()
        .then((results) => {
 if (results.items.length > 0) {
 let items = results.items;

 const firstWrods = results.items.map(e => e.hashTags.split(" ")[0]);
 
 
          console.log("first words  " + firstWrods)

            }
        });

One more thing !!
I am having a repeater in my page.
In the repeater 2 texts.
I am getting the first words from the field “hashTags”.
Now, in the database, there is another text field called “name”.

When I get the first words, can I also get the corresponding value from the field “name” and assign the both to repeater.

@ajithkrr

wixData.query("userCreations")
.isNotEmpty("hashTags")
.find()
.then(r => {
let items = r.items;
if(items.length > 0){
items = items.map(e => {e.firstWord = e.hashTags.split(" ")[0];  return e;})
$w("#repaeater1").data = items;
}
})
$w("#repeater1").onItemReady(($i, iData) => {
$i("#firstWordText").text = iData.firstWord;
$i("#nameText").text = iData.name;
})

@jonatandor35 Thanks J.D!!!
A very thanks for your valuable time and resources .
It worked !!!

wixData.query("userCreations")
        .isNotEmpty("hashTags")
        .find()
        .then((r) => {
 if (r.items.length > 0) {
 let items = r.items;
                items = items.map(e => { e.firstWord = e.hashTags.split(" ")[0]; return e; })
                console.log(items);

                $w('#repeater1').data = items;

                $w('#repeater1').onItemReady(($i, iData) => {
                    $i('#text58').text = iData.firstWord;
                    $i('#text59').text = iData.creation;
                });

            }
        });

You’re welcome :).
I think it’s better practice to put the onItemReady() outside the .then() part

@jonatandor35 Sure!!!