Retrieve child element without knowing the parent - javascript

I would like to know if it's possible to get the name of the objet1 & objet2 without specifying 'objet1' & 'objet2'?
let tableauObj = [
objet1: {name: Albert},
objet2: {name: Florence}]

You can refer to objects in an array by their index within the array. Click these links to learn more about objects and arrays in JavaScript.
let tableauObj = [
{name: "Albert"},
{name: "Florence"}];
// The object with the name property of "Albert"
// is at index zero of the tableauObj array:
console.log(tableauObj[0].name);
// The object with the name property of "Florence"
// is at index one of the tableauObj array:
console.log(tableauObj[1].name);

Related

Remove element from array of objects if the element will be inserted again

First of all hello!
I got an array of objects like this in JS:
[{name: "pepe", id: "#34"}{name: "juan", id: "#23"}]
And i want remove an object from the array if the user tries to insert the item again.
Example
Original array:
[{name: "pepe", id: "#34"}{name: "juan", id: "#23"}]
User clicks to add pepe again. Result:
[{name: "juan", id: "#23"}]
If someone could help me it will be great!
You have to find the position of the element by using :
index = array_name.findIndex(obj => obj.name === "pepe");
After that, you can use
array_name.splice(index, 1);
Before using this function, you need to check if the index is not -1.
If you get it, it means the object is not in your list and you should not execute the function. Otherwise, you will get an error.

AngularJS Get all values from an array in Javascript and push it to another array

var items = [
{name: 'Tyres'},
{name: 'Computers'},
{name: 'Leather'},
{name: 'Furniture'},
{name: 'Electronics'}
];
I want to print this array or console.log it in javascript like this.
Tyres,Computers,Leather,Furniture,Electronics
I dont want the key from this array. I just want to print values from this array. kindly help me with this. Thanks
You want to do something like the following. Firstly, get all the names by using map and then use join(',') to turn it from an array into a string and separate each item with a comma.
const items = [
{name: 'Tyres'},
{name: 'Computers'},
{name: 'Leather'},
{name: 'Furniture'},
{name: 'Electronics'}
];
console.log(items.map(i => i.name).join(','));
the easiest way is to map the items, returning only the desired parameter value:
in es6:
console.log(items.map(i => i.name));
in older js:
console.log(items.map(function(i) { return i.name; }));
edit (for op's comment):
to get just a string of the values, try using join. you can chain it right onto the end of the map function like so:
console.log(items.map(i => i.name).join(','));
this will join the array values into a single string, using the delimiter specified to separate the values.

Check if an array or objects has a specific key

I'm trying to check if an object in an array has a key before performing a map.
My initial object array looks like:
[{label: "", value: ""}, {label: "", value: ""}]
I have a method which changes the above array so that I can post it back. This then looks like:
["STRING","STRING"]
The method I'm trying is:
var returiningUsers = [];
if (this.state.users.length > 0) {
returiningUsers = this.state.users.map(user => user.value)
console.log('has users with value');
}
return returiningUsers
The above works if there are 2 or more items in the array. When the array structure changes I get the following error: TypeError: _this.state.users.map is not a function.
I need to check if the object array key exists before doing the map function. Is this possible?
Javascript has an object property for this
myObj.hasOwnProperty('key')
I'm not sure what you mean by "changes the structure" but you can also just filter the array for objects that have the key you are looking for
if (this.state.users.length > 0) {
returiningUsers = this.state.users.filter(user => user.hasOwnProperty('value'))
}

How to access value from the object within an array?

How can I access Name's value and assign it to an variable?
var arr = [
{Name: "Jason",
Title: "Student",
Image: "asdf",
Status: "Happy"}
];
Try this:
var [{Name: name}] = arr;
This uses ES6 destructuring assignment.
First, the outermost [] is a way of referring to an array on the right hand side (in this example, arr). Things placed within these square brackets (here's there's only one) refer to the first, second, and succeeding values of that array. So here, the {Name: name} portion refers to the first (0th) element of the array. In other words, it is equivalent to
var {Name: name} = arr[0];
The inner {} is a way of referring to objects and picking them apart. {Name: name} says to find the Name property of the object being picked apart. Then the : name part says to rename it to name. Since all this is occurring in the context of a var statement, the result is declare a new variable with the name name and assign the value being picked out to it.
Here's the more detailed sequence:
var // Start a variable declaration
[ // Pick apart an array on the RHS, starting with 1st element
{ // Pick apart an object
Name // Find the property named `Name` in that object
:name // Rename it; this is the variable that will be declared!
} // Done picking apart the object
] // Done picking apart the array
= arr; // Specify the thing to deconstruct
Access the element at index 0 of array using bracket notation, then access property name Name of object using dot or bracket notation
var arr = [
{Name: "Jason",
Title: "Student",
Image: "asdf",
Status: "Happy"}
];
var name = arr[0].Name; // alternatively, `arr[0]["Name"]`
var arr = [
{Name: "Jason",
Title: "Student",
Image: "asdf",
Status: "Happy"}
];
var myname = arr[0]['Name'];
console.log(myname);

how to access object inside an object Array which is inside another array in javascript?

I have an array as follows
[
[{"Id":"5","Color":"White"}],
[{"Id":"57","Color":"Blue"}],
[{"Id":"9","Color":"Brown"}]
]
each object is inside an array which is inside another array. I want to access an object item, let say 'Id' of first object ("Id":"5"). How can I do that?
If the array is assigned to a variable:
var a = [
[{"Id":"5","Color":"White"}],
[{"Id":"57","Color":"Blue"}],
[{"Id":"9","Color":"Brown"}]
];
You can do it like this:
a[0][0].Id;
or
a[0][0]["Id"];
To get the second object you would do:
a[1][0].Id;
or
a[1][0].["Id"];
if it's javascript your object must be named (e.g. x)
Then select the index of the first array (here : 0, 1 or 2)
Then the "small" array content only one item, you have no choice, take 0.
For end, you can pick the property you need, Id or Color.
You have :
var myColor = x[1][0]["Color"];
console.log(myColor); //output : Blue
var obj_c = [
[{"Id":"5","Color":"White"}],
[{"Id":"57", "Color": "Blue"}],
[{"Id":"9","Color":"Brown"}]
];
console.log(obj_c[0][0].Id);
console.log(obj_c[0][0].Color);

Categories