-2

I have an array of object with a few values. I would like to get only last, three values (ownerSurname, color and bark).

I tried to use slice or length but with failure.

const myValues: MyValues<string>[] = [
   {
    name: "Lucky",
    surname: "Luck",
    age: "3",
    breed: "Shepherd",
    owner: "Paul",
    ownerSurname: "Luck",
    color: "Brown",
    bark: "All the time",
  },
]
const newData = myValues.slice(-3)
<MyComponent value={newData}/>
10
  • 3
    That should be throwing some errors in TypeScript, because what is shown is not an array of strings; it's an array of a single object. Commented Jul 8 at 13:44
  • Do you want the last three elements in an array? Or do you want an array of objects which are a subset of the properties of the objects in your original array? It's not really clear from the attempt. (This seems to have less to do with TypeScript or React and more to do with simply objects and arrays in JavaScript, and generally understanding your own data structure...)
    – David
    Commented Jul 8 at 13:47
  • Actually, I was mistaken. There are no TypeScript errors, because we don't know what MyValues is. Commented Jul 8 at 13:49
  • My data is an array that has only one object whose values I need to extract (only the last three).
    – Emm
    Commented Jul 8 at 13:51
  • 3
    Why not just get [myValues[0].ownerSurname, myValues[0].color, myValues[0].bark] then? Commented Jul 8 at 14:08

2 Answers 2

0

I can only guess what you actually want to achieve, so ill provide some different solutions.

The thing i think you want to do is, only get the values "ownerSurname", "color" and "bark" for each of your objects in your array. That could be achieved like this:

[{name: "name", age: 20}].map(value => ({name: value.name}))

As explanation: This code maps through every object, and then returns a new object with only the "name" field in it. You could change this code to your specific example.

I can also imagine, that you might want to omit all other values from only one object. This might be a misunderstanding when coming from other languages like phps where objects are handled with brackets. You could just do that by removing the brackets from your "myValues" like this:

const myObject = {name: "name", age: 19}
const myNewObject = {name: myObject.name}

Please note that slice takes the last 3 values of an array, not an object. Please see the difference here: https://medium.com/@zac_heisey/objects-vs-arrays-42601ff79421

2
  • Thank you. It means that I have an array of objects and I would like to work on the data of this array it refers to the objects so I should using methods for objects instead of arrays right?
    – Emm
    Commented Jul 8 at 14:02
  • yes you're right! First of all you need to get your object from the array though.
    – Lucky
    Commented Jul 12 at 7:19
0

This is more related to simple Javascript rather than React.

If you really would like to know, first you need to "get" the object itself, in this case because is an array with one single element it is only a matter of select the [0] index.

const myValues: MyValues<string>[] = [
   {
    name: "Lucky",
    surname: "Luck",
    age: "3",
    breed: "Shepherd",
    owner: "Paul",
    ownerSurname: "Luck",
    color: "Brown",
    bark: "All the time",
  },
]

const obj = myValues[0];

const newObj = { 
  ownerSurname: obj.ownerSurname, 
  bark: obj.bark, 
  color: obj.color 
};

<MyComponent value={newObj}/>

These are basic functions of javascript and are not related to React. Perhaps you need to find more basic lessons on how objects and arrays work in Javascript.

Not the answer you're looking for? Browse other questions tagged or ask your own question.