This depends on a few things like where these objects are coming from. If the data is coming from a server, you can truncate the names on the server side before they get to the client side. If you don't have access to the server code, you can truncate truncate the names on the client side yourself:
const maxLength = 50;
const resultArray = [{ ... }].map(i => {
if (i.name <= maxLength) return i;
const shortenedName = i.name.substring(0, maxLength + 1);
i.name = shortenedName + '...';
return i;
});
Sorry, I missed the second question you had. If you want to be able to still access the full name, you'll need to modify the loop above so that it doesn't overwrite the name, but instead, store a second value for short name:
const rawData = [
{
name: 'realllyyyyyyLongggname',
age: '28',
hobbit: 'blah'
},
{
name: 'anotherrealllyyyyyyLongggname',
age: '28',
hobbit: 'blah'
}
];
const maxLength = 10;
const resultArray = rawData.map(i => {
if (i.name <= maxLength) i.shortName = i.name;
else {
const shortenedName = i.name.substring(0, maxLength + 1);
i.shortName = shortenedName + '...';
}
return i;
});
console.log(resultArray);
this way, you can give the 'name' to the title attribute, and the 'shortName' to other places that need it