0

I have this scenario, but it's not working...
Under h1 I want to show all elements from my array and in input value I want to show the last element from my array:

<h1>Solutions></h1>
<%  var element = title.forEach(function(item) { %>
    <p><%= item.name%></p>
    <% }) %>
<form action="/" method="post">
    <input type="text" name="name" />   
    <input type="text" name="s" placeholder="solution subtitle" 
    value=
    <%= element.length-1 %>

    />
    <button type="submit" name="submit">Submite</button>
</form>

How can I get the last element from array using eJS?

7
  • Hi there! The last element in an array should be accessible like this: array[array.length - 1]. Are you trying to get the last element of the title or element array? Commented Mar 23, 2020 at 9:27
  • last elemet from title Commented Mar 23, 2020 at 9:32
  • Ok. You can access that like this: titile[titile.length - 1]. In the input element, that would be something like this: <input type="text" name="s" placeholder="solution subtitle" value= <%= titile[titile.length - 1]%> Commented Mar 23, 2020 at 9:40
  • I tried that one too but also something not working ... it's return "{" symbol in input Commented Mar 23, 2020 at 9:53
  • { "_id" : ObjectId("5e74c2ff6ee4a61a0ca3a2f0"), "name" : "someItem1", "__v" : 0 } { "_id" : ObjectId("5e787701e5b6955328ffce26"), "name" : "someItem2", "__v" : 0 } { "_id" : ObjectId("5e787704e5b6955328ffce27"), "name" : "someItem3", "__v" : 0 } { "_id" : ObjectId("5e787706e5b6955328ffce28"), "name" : "someItem4", "__v" : 0 } I want to get "someItem4" element Commented Mar 23, 2020 at 9:54

1 Answer 1

1

You may use like this

 <%= element[element.length-1] %>
Sign up to request clarification or add additional context in comments.

1 Comment

<%= title[title.length-1] %>

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.