Create sentence with data from two different objects - concatenation

Based on this data:
const employees = {
employee1: {name: "Susana", age: 54},
employee2: {name: "Mariano", age: 55}
}
const companies = {
company1: {name: "General Electric"},
company2: {name: "Apple Computers"}
};
I need to create a sentence that looks like:
Susana works at General Electric
Since both objects use the value name (and I am not allowed to change them), I have no idea how to concatenate both to create a coherent sentence. How can I do it?

const createSentence = function (name, company) {
let nombre = employees.employee1['name'];
let compania = companies.company1['name'];
return console.log(nombre + " works at " + compania);
}
createSentence(); // "Susana works at General Electric"

Related

I'm trying to understand reduce and i'm scratching my head about this, why is this code not giving the expected result

I am reduce an array of object into a single object that should look like this :
`
result = {
23 : [{obj of kyle}, {obj of jade}],
29 : [{obj of ruby}]
32 : [{obj of mat}]
}
`
I have used reduce to do this, each person variable will refer to an object of the array to reduce, and they all collapse into a single object refered to with the variable group, the initial value of that object is an empty object {}, so with an if statement i checked first if it's empty, then create a key named age with a new empty array as value and push the person object into that empty array, and if the key age exists, then skip the new array creation and push that corresponding person object into the corresponding array.
what's wrong with this code?
`
const people = [
{name: "kyle", age: 23},
{name: "jade", age: 23},
{name: "ruby", age: 29},
{name: "mat", age: 32}
]
let result = people.reduce(function(person, group){
const age = person.age;
if(group[age]==null){
group[age] = []
}
group[age].push(person);
return group
},{})
console.log(result);
`
not what i expected
Looking again at your code it seems that you can fix your own code by replacing person and group in your callback function inside reduce like so
const people = [
{name: "kyle", age: 23},
{name: "jade", age: 23},
{name: "ruby", age: 29},
{name: "mat", age: 32}
]
let result = people.reduce(function(group, person){
const age = person.age;
if(group[age]==null){
group[age] = []
}
group[age].push(person);
return group
},{})
console.log(result);
You can do something like this as well
const people = [
{name: "kyle", age: 23},
{name: "jade", age: 23},
{name: "ruby", age: 29},
{name: "mat", age: 32}
];
const result = people.reduce((acc, curr) => {
if (!acc[curr.age]) {
return {...acc, [curr.age]: [curr]}
}
return {...acc, [curr.age]: [...acc[curr.age], curr]};
}, {});
console.log(result);
What we did here is initiating it with a new object like you did, then for each element in the array denoted as curr for current we check if our new object (denoted as acc for accumulator) has a key with this age already of curr.age. If not we make an array for this age and put the current element inside.
Else, if this age key already exist just add to that age array the current element.

Draw table from js file using js

I am trying to display all info from an array to a table. This is my javascript array:
JS
let tableUsers = [
{name: "Jose", age: "22", Country: "Spain"},
{name: "Jon", age: "25", Country: "France"},
{name: "Jacob", age: "36", Country: "Italy"}
]
I think you are misunderstanding of variable definition.
In ES6, we use let keyword to define variable and is valid in the scope of bracket in which it is defined.
So the code should be following.
let tableUsers = document.getElementById("tableDiv");
function drawTable(){
let table = document.createElement("table");
let tableHead = document.createElement("thead");
let colHeads = ["column1", "column2"];
for (let header of colHeads){
let celle = document.createElement("th")
celle.innerHTML = header;
tableHead.appendChild(celle);
}
table.appendChild(tableHead)
let rad; // defined out of the for loop
for(let x of info){
rad = document.createElement("tr");
let firstname = document.createElement("td");
firstname.innerHTML = x.name.first;
rad.appendChild(firstname);
}
table.appendChild(rad)
}
drawTable()

Swift - Check if a value belongs is in an array

I have an array of type "User" and I would like to check if a value belongs to a property type.
My code :
struct User: Identifiable {
var id = UUID()
var name: String
var age: String
}
var array: User = [
User[name: "AZE", age: "10"]
User[name: "QSD", age: "37"]
]
For example I'd like to know if "AZE" belongs to the property array "name". What is the function for retrieving this information. I hope you understood my problem and thank you for your answer.
First of all, arrays define with [Type] like [User]
Second of all init method calls as with (Arguments) like User(name: ,age:)
And last but not least, don't forget the ',' between elements of the array.
So
struct User: Identifiable {
var id = UUID()
var name: String
var age: String
}
var array: [User] = [
User(name: "AZE", age: "10"),
User(name: "QSD", age: "37")
]
So now you can check your element inside with contains like
array.contains(where: { user in user.name == "AZE" }) // returns `true` if it is
Tips
Try name arrays not array. Use plural names instead like users
To returtning the found one:
users.first(where: { user in user.name == "AZE" })
To summarizing it
users.first { $0.name == "AZE" }

How to convert an array of dictionaries into an array of keys using react

Given a list:
let names = [{name: "bobby"}, {name: "sydney"}, {name: "Paul"}, {name: "Grace"}
I want the output to be ["bobby", "sydney", "Paul", "Grace"]
Here is what I have tried:
var items = Object.keys(names).map(function(i) {
return names[i];
})
const items = Object.keys(names).map((key)=>names[key]);
this.setState({items});
console.log(this.state.items);
names.map(({ name }) => name)
const names = [{
name: "bobby"
}, {
name: "sydney"
}, {
name: "Paul"
}, {
name: "Grace"
}];
const keys = names.map(({
name
}) => name);
console.log(keys);
A note about react keys, they should be unique within the rendered siblings, i.e. they should be unique within the dataset. Names alone may not provide sufficient uniqueness.
A second note, you might not want to generate your react keys separately from where you need them, i.e. generally they are created when you are mapping JSX.
This is not really related to React. You can do that with JavaScript, for instance using API like map().
Here is an example:
let arr = names.map(obj => obj.name);

Add an Array of length x to value in key : value hash using Ruby

Here is a hash
personal_details = {
name: name,
dob: dob,
age: age,
height: height
}
Here is an array
puts "What are the name of some of your relatives? (eg. Bob, James, Harry etc)"
relatives = gets().chomp.split(",")
Lets say the input for relatives was "Bob, James, Harry".
I want the hash to look like this:
personal_details = {
name: name,
dob: dob,
age: age,
height: height
relatives: [
{name: Bob},
{name: James},
{name: Harry}
]
}
The array in the hash is updated based on user input.
Start by initialising relatives as an array:
personal_details = {
name: name,
dob: dob,
age: age,
height: height,
relatives: []
}
Then for each relative, push to the array:
relatives = gets().chomp.split(",")
relatives.each do |relative|
personal_details.relatives.push(name: relative)
end
This should work, if I've interpreted the question as intended:
personal_details[:relatives] = relatives.map{|r| {name: r}}

Resources