Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Array - get first 10 items [duplicate]

Tags:

javascript

I have got a javascript array, and I want to extract the first 10 items from it. Is this possible to use map method for this?

the code I tried is below:

data.map((item, i) => {
  placeIDs.push(item.place_id);
});

This code block returns 20 place_ids but I want to have only 10.

So I tried break inside the map method, and it says SyntaxError: Illegal break statement.

Here data is json result from google place api, and it returns 20 results.

One result

{
    "geometry": {
      "location": {
        "lat": 61.2180556,
        "lng": -149.9002778
      },
      "viewport": {
        "northeast": {
          "lat": 61.48393789999999,
          "lng": -148.4600069
        },
        "southwest": {
          "lat": 60.733791,
          "lng": -150.4206149
        }
      }
    },
    "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
    "id": "2edc2d446c844a7a55a2ccbe4a2dfda60a5a0264",
    "name": "Anchorage",
    "photos": [
      {
        "height": 1152,
        "html_attributions": [
          "<a href=\"https://maps.google.com/maps/contrib/102061280252416580615\">Efren Norstein</a>"
        ],
        "photo_reference": "CmRaAAAA9aiV4_BwvK6GfpuswWMBzwuO4LM55YUxGuN8q-4kyZ2-eeyl386ArGmc0-qyBr1r49cuibTIx_2QjFNIBoSRZFspgTBKzciji_-srPClBjNKx8q02BmvwM5vZxVy71lSEhDSY8VwSU2I6uHJPBVvZStBGhQ-_-ZcvP8QhktxugB9k_YHr3OX6A",
        "width": 2048
      }
    ],
    "place_id": "ChIJQT-zBHaRyFYR42iEp1q6fSU",
    "reference": "ChIJQT-zBHaRyFYR42iEp1q6fSU",
    "scope": "GOOGLE",
    "types": ["locality", "political"],
    "vicinity": "Anchorage"
  },

placeIDs is an array. I want to extract place_ids from the first 10.

So the main idea is that is it possible to break the map inside it?

like image 563
Kid Avatar asked Dec 06 '19 09:12

Kid


People also ask

How can we get the first 10 elements of an array?

Use the Array. slice() method to get the first N elements of an array, e.g. const first3 = arr. slice(0, 3) . The slice() method will return a new array containing the first N elements of the original array.

How do you find repeating elements in an array?

Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.

How do you find duplicate numbers in an array if it contains multiple duplicates JavaScript?

Using the indexOf() method In this method, what we do is that we compare the index of all the items of an array with the index of the first time that number occurs. If they don't match, that implies that the element is a duplicate. All such elements are returned in a separate array using the filter() method.

Can array have duplicate values JavaScript?

With ES6, we have a javascript Set object which stores only unique elements. A Set object can be created with array values by directly supplying the array to its constructor. If the array has duplicate values, then they will be removed by the Set. This means that the Set will only contain unique array elements.


2 Answers

This will do the job. Replace [startIndex] and [endIndex] with 0 and 10 to get the first 10.

data.slice([startIndex], [endIndex]).map((item, i) => {
  placeIDs.push(item.place_id);
});
like image 111
Thor Jacobsen Avatar answered Oct 26 '22 06:10

Thor Jacobsen


Take a look at slice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

E.g. const my10Items = myArray.slice(0, 10);

like image 8
Mick Avatar answered Oct 26 '22 07:10

Mick