Related
I have 2 arrays with objects.
const users1 = [
{ name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' },
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
],
const users2 = [
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
],
I need to get index from users1 of that parts that are contains in users2.
As a result I should get [{1: true}, {3: true}].
I try in this way but it's not correct
const arr = user1.filter((item, index) => user2.map(i) => {
if(i.id === item.id) return {index: true}
return null;
})
You could create a Set of all ids from users2 (a Set is somewhat like an array, but allows you to quickly check if a value is in it using .has() and it only stores unique values). Once you have the set, you can use .reduce() to create a new array, where you can use .concat() if the set has the id of the object from users1:
const users1 = [{ id: 1, name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' }, { id: 2, name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' }, { id: 3, name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' }, { id: 4, name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }, ];
const users2 = [{ id: 2, name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' }, { id: 4, name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }, ];
const u2Ids = new Set(users2.map(user => user.id));
const res = users1.reduce((acc, obj, i) => u2Ids.has(obj.id) ? acc.concat({[i]: true}) : acc, []);
console.log(res);
When creating your objects you need to ensure you use computed property names when adding the index as a key {[i]: true} so that the value at i is used rather than the literal i name. The above can also be written with a for..of loop (by accessing Array.prototype.entries()), which is more efficient and (in my opinion) more readable:
const users1 = [{ id: 1, name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' }, { id: 2, name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' }, { id: 3, name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' }, { id: 4, name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }, ];
const users2 = [{ id: 2, name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' }, { id: 4, name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }, ];
const res = [];
const u2Ids = new Set(users2.map(user => user.id));
for(const [i, obj] of users1.entries()) {
if(u2Ids.has(obj.id))
res.push({[i]: true});
}
console.log(res);
First your array don't contains ids I filtered name for now.Also filter will not work here bcs if you use filter that will just filter your array and index will no longer be the same try following instead
const users1 = [
{ name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' },
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }
];
const users2 = [
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }
];
const arr = [];
users1.forEach((item, idx) =>{
if(users2.some(u=> u.name == item.name)){
arr.push({[idx]:true})
}
});
console.log(arr)
Just another approach - with Array.reduce()
const users1 = [
{ name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' },
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
];
const users2 = [
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
];
const arr = users1.
reduce((previousValue, currentValue, index) => {
// change here to find by id
if (users2.some(u2 => u2.name === currentValue.name) ){
// map to whatever you want
const valueToSave = {[index] : true};
return [...previousValue, valueToSave];
} else {
return previousValue;
}
}, []);
console.log(arr);
#festina, to keep it simple we map on users2, as we are trying to find objects of users1 in users2, we get index using findIndex using (id/name any unique key available) and then filter by index > 0, and format the result in required format
const users1 = [
{ name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' },
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
];
const users2 = [
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
];
// your solution
const result = users2.map((user, index) => users1.findIndex(u => u.name === user.name))
.filter(index => index > 0)
.map((i => { return { [i]: true} }));
console.log(result)
Interface -
interface I {
name: string;
age: number;
city: string;
address?: string;
}
Arrays -
const arr1: I[] = [
{
name: "daniel",
age: 21,
city: 'NYC'
},
{
name: "kosta",
age: 28,
city: "NYC"
},
{
name: "yoav",
age: 28,
city: "NYC"
}
];
const arr2: I[] = [{
name: "daniel",
age: 21,
city: "NYC",
address: 'E. 43'
},
{
name: "simon",
age: 24,
city: "NYC",
address: 'E. 43'
},
{
name: "david",
age: 22,
city: "NYC",
address: 'E. 43'
},
{
name: "kosta",
age: 28,
city: "NYC",
address: 'E. 43'
}
];
Getting keys for the arrays -
const arr1Map: ReadonlyMap<string, string | undefined> = new Map(
arr1.map(
({
name, age, city, address
}) => [
`${name}|${age}|${city}`,
address
]
)
);
const arr2Map: ReadonlyMap<string, string | undefined> = new Map(
arr2.map(
({
name, age, city, address
}) => [
`${name}|${age}|${city}`,
address
]
)
);
Empty arrays -
let arr1Match: I[] = []
let arr1Unmatch: I[] = []
let arr2Match: I[] = []
let arr2Unmatch: I[] = []
What I need to do now is to campare all values in arr1 to arr2, if there is a match, store the match from arr1 in arr1Match and the match from arr2 in arr2Match. If there is an Unmatch I need to store the unmatch arr1 in arr1Unmatch and the unmatch from arr2 in arr2Unmatch.
And if there is a match I need to store the address from arr2 into arr1.
The desierd output -
arr1Match: [{ name: "daniel", age: 21, city: "NYC", address: 'E. 43' }, { name: "kosta", age: 28, city: "NYC", address: 'E. 43' } ]
arr2Match: [{ name: "daniel", age: 21, city: "NYC", address: 'E. 43' }, { name: "kosta", age: 28, city: "NYC", address: 'E. 43' }]
arr1Unmatch: [{ name: "yoav", age: 28, city: "NYC" }]
arr2Unmatch: [{ name: "simon", age: 24, city: "NYC", address: 'E. 43' }, { name: "david", age: 22, city: "NYC", address: 'E. 43' }]
The answer depends on some questions about your needs: What constitutes a match? If there's different data between the matches, what should be put in the match array? Should the arrays point to the original objects, or to copies of them?
Also, it looks like there is no difference between arr1Match and arr2Match, so those can be combined into one
Either way the solution would be to iterate over one array, and search for a match in the other array for every value. Any item that doesn't match will go to the unmatch arrays
// Replace with real match logic
const isMatch = <T>(a: T, b: T) => Math.random() < 0.5;
const getMatches = <T>(arrOne: T[], arrTwo: T[]) => {
const matches: T[] = [];
const arrOneUnmatches: T[] = [];
let arrTwoUnmatches: T[];
// Copying for comfortability's sake
const arrTwoCopy = [...arrTwo];
arrOne.forEach(item => {
// Find a match in array two
const arrTwoMatchIndex = arrTwoCopy.findIndex(arrTwoItem => isMatch(item, arrTwoItem));
if (arrTwoMatchIndex) {
matches.push(item);
// Remove it from arrTwoCopy, to maintain arrTwoUnmatches
arrTwoCopy.splice(arrTwoMatchIndex, 1);
} else {
// No match = go to arrOneUnmatches
arrOneUnmatches.push(item);
}
})
// Anyone left in arrTwoCopy didn't match anyone in arrOne, so they have no match
arrTwoUnmatches = arrTwoCopy;
return { matches, arrOneUnmatches, arrTwoUnmatches }
}
I am attempting to check whether all age groups from teen to centenarian are represented in an array.
My logic path has been:
Create an array with pre-defined decade-based age brackets [1, 2, 3, ... , 10]
Isolate the age value of each element in the sample array of objects
Divide those ages by 10 to get what decade the age falls in
parseInt that number to get an integer and put resultant figures in new array
Compare that array with the pre-defined age bracket array created initially.
The expected output is true but I am instead getting false.
Another logic issue I am facing is that the oldest age in the sample array, 128, reduces to 12 after dividing/parsing whereas it would be preferable for it to check if the int is > 10 instead of specifically 10.
Code below:
const devAges = list.map((list) => {
return list.age
})
const devAgesDiv = devAges.map(i => i / 10);
for(i = 0; i < devAgesDiv.length; i++){
devAgesDiv[i] = parseInt(devAgesDiv[i]);
}
function allAges(list) {
const ageBrackets = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
return ageBrackets.every((ageBracket) =>
list.some((listItem) => listItem.ageBracket === ageBracket)
);
}
console.log(allAges(list)); // output false;
Sample array below:
var list = [
{ firstName: 'Harry', lastName: 'K.', country: 'Brazil', continent: 'Americas', age: 19, language: 'Python' },
{ firstName: 'Kseniya', lastName: 'T.', country: 'Belarus', continent: 'Europe', age: 29, language: 'JavaScript' },
{ firstName: 'Jing', lastName: 'X.', country: 'China', continent: 'Asia', age: 39, language: 'Ruby' },
{ firstName: 'Noa', lastName: 'A.', country: 'Israel', continent: 'Asia', age: 40, language: 'Ruby' },
{ firstName: 'Andrei', lastName: 'E.', country: 'Romania', continent: 'Europe', age: 59, language: 'C' },
{ firstName: 'Maria', lastName: 'S.', country: 'Peru', continent: 'Americas', age: 60, language: 'C' },
{ firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 75, language: 'Python' },
{ firstName: 'Chloe', lastName: 'K.', country: 'Guernsey', continent: 'Europe', age: 88, language: 'Ruby' },
{ firstName: 'Viktoria', lastName: 'W.', country: 'Bulgaria', continent: 'Europe', age: 98, language: 'PHP' },
{ firstName: 'Piotr', lastName: 'B.', country: 'Poland', continent: 'Europe', age: 128, language: 'JavaScript' }
];
You can make two updates to get the desired result:
devAgesDiv - check if the list age is over 100 and return 10 otherwise divide by 10; you can parseInt the result of that test
You can use includes in allAges to check every ageBracket is represented
See below:
const list = [
{ firstName: 'Harry', lastName: 'K.', country: 'Brazil', continent: 'Americas', age: 19, language: 'Python' },
{ firstName: 'Kseniya', lastName: 'T.', country: 'Belarus', continent: 'Europe', age: 29, language: 'JavaScript' },
{ firstName: 'Jing', lastName: 'X.', country: 'China', continent: 'Asia', age: 39, language: 'Ruby' },
{ firstName: 'Noa', lastName: 'A.', country: 'Israel', continent: 'Asia', age: 40, language: 'Ruby' },
{ firstName: 'Andrei', lastName: 'E.', country: 'Romania', continent: 'Europe', age: 59, language: 'C' },
{ firstName: 'Maria', lastName: 'S.', country: 'Peru', continent: 'Americas', age: 60, language: 'C' },
{ firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 75, language: 'Python' },
{ firstName: 'Chloe', lastName: 'K.', country: 'Guernsey', continent: 'Europe', age: 88, language: 'Ruby' },
{ firstName: 'Viktoria', lastName: 'W.', country: 'Bulgaria', continent: 'Europe', age: 98, language: 'PHP' },
{ firstName: 'Piotr', lastName: 'B.', country: 'Poland', continent: 'Europe', age: 128, language: 'JavaScript' }
];
const devAgesDiv = list.map(item => parseInt(item.age > 100 ? 10 : item.age / 10));
const ageBrackets = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function allAges(list) {
return ageBrackets.every(ageBracket => list.includes(ageBracket));
}
console.log(allAges(devAgesDiv)); // true
What is wrong in this if condition. I am getting the wrong result. I need to get equal values in these two objects and diff between them.
const firstArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 23, city: 'Berlin' }, { name: 'Sara', age: 28, city: 'Paris' }, { name: 'Rash', age: 20, city: 'Dubai' } ];
const secondArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 27, city: 'Berlin' }, { name: 'Hary', age: 29, city: 'London' }, ];
for (let i = 0; i < firstArr.length; i++) {
for (let j = 0; j < secondArr.length; j++) {
if (firstArr[i].name == secondArr[j].name) {
console.log('eq', firstArr[i].city, secondArr[j].city)
}
if (firstArr[i].name != secondArr[j].name) {
console.log('not found in second array', firstArr[i].city)
}
if (secondArr[j].name != firstArr[i].name) {
console.log('not found in first array', secondArr[j].city)
}
}
}
Currently you compare each element of the first array with each element of the second array. You could instead use Array.prototype.some and Array.prototype.every to filter the arrays and to find the intersection resp. difference. Then you can map the objects to the city names.
const firstArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 23, city: 'Berlin' }, { name: 'Sara', age: 28, city: 'Paris' }, { name: 'Rash', age: 20, city: 'Dubai' } ];
const secondArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 27, city: 'Berlin' }, { name: 'Hary', age: 29, city: 'London' }, ];
function intersect(lhs, rhs) {
return lhs.filter(el => rhs.some(el2 => el.name === el2.name)).map(el => el.city);
}
function diff(lhs, rhs) {
return lhs.filter(el => rhs.every(el2 => el.name !== el2.name)).map(el => el.city);
}
console.log(intersect(firstArr, secondArr));
console.log(diff(firstArr, secondArr));
console.log(diff(secondArr, firstArr));
I loop the first array first and find matches in second array. If there is a match, diff is displayed. If there is no such match, then the correct text is being displayed. An array is built along the way, which is used to simplify the loop on the second array.
const firstArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 23, city: 'Berlin' }, { name: 'Sara', age: 28, city: 'Paris' }, { name: 'Rash', age: 20, city: 'Dubai' } ];
const secondArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 27, city: 'Berlin' }, { name: 'Hary', age: 29, city: 'London' }, ];
let names = [];
for (let first of firstArr) {
let matches = secondArr.filter((second) => (first.name === second.name));
if (matches.length) {
console.log('eq', first.city, matches[0].city)
} else {
console.log('not found in second array', first.city);
}
names.push(first.name);
}
for (let second of secondArr) {
if (names.indexOf(second.name) === -1) console.log('not found in first array', second.city);
}
Try this:
const firstArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 23, city: 'Berlin' }, { name: 'Sara', age: 28, city: 'Paris' }, { name: 'Rash', age: 20, city: 'Dubai' } ];
const secondArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 27, city: 'Berlin' }, { name: 'Hary', age: 29, city: 'London' }, ];
var eq = [], uniqueInFirst = [], uniqueInSecond = [];
for (let i = 0; i < firstArr.length; i++) {
var secondArrCities = Object.values(secondArr).map ((obj) => {return obj.city})
if (secondArrCities.includes(firstArr[i].city)) {
eq.push(firstArr[i].city)
} else {
uniqueInFirst.push(firstArr[i].city)
}
}
for (let i = 0; i < secondArr.length; i++) {
var firstArrCities = Object.values(firstArr).map ((obj) => {return obj.city})
if (!firstArrCities.includes(secondArr[i].city)) {
uniqueInSecond.push(secondArr[i].city)
}
}
console.log(eq)
console.log(uniqueInFirst)
console.log(uniqueInSecond)
Is it possible to iterate over the array, excluding the first element (omit the first object in the array)?
CODE:
let multipleDemo =[];
let people = [
{ name: 'Adam', email: 'adam#email.com', age: 12,
country: 'United States' },
{ name: 'Amalie', email: 'amalie#email.com', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: 'estefania#email.com', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: 'adrian#email.com', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: 'wladimir#email.com', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: 'samantha#email.com', age: 30,
country: 'United States' },
{ name: 'Nicole', email: 'nicole#email.com', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: 'natasha#email.com', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: 'michael#email.com', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: 'nicolas#email.com', age: 43,
country: 'Colombia' }
];
for(var i =0; i < people.length; i++) {
multipleDemo.push(people[i]);
people.splice(people[i], 1000);
console.log(multipleDemo);
console.log(people);
}
Example code: https://plnkr.co/edit/UJfRUs6dAT1NC1EnOvqA?p=preview
I want to leave { name: 'Adam', email: 'adam#email.com', age: 12, country: 'United States' } in array people. Rest of elements I want to put in array multipleDemo
I want such as FINISH EFFECT:
let multipleDemo =[,
{ name: 'Amalie', email: 'amalie#email.com', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: 'estefania#email.com', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: 'adrian#email.com', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: 'wladimir#email.com', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: 'samantha#email.com', age: 30,
country: 'United States' },
{ name: 'Nicole', email: 'nicole#email.com', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: 'natasha#email.com', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: 'michael#email.com', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: 'nicolas#email.com', age: 43,
country: 'Colombia' }];
let people = [
{ name: 'Adam', email: 'adam#email.com', age: 12,
country: 'United States' }
];
You can use Array.prototype.slice() to modify your arrays to get your desired output.
let people = [{
name: 'Adam',
email: 'adam#email.com',
age: 12,
country: 'United States'
},
{
name: 'Amalie',
email: 'amalie#email.com',
age: 12,
country: 'Argentina'
},
{
name: 'Estefanía',
email: 'estefania#email.com',
age: 21,
country: 'Argentina'
},
{
name: 'Adrian',
email: 'adrian#email.com',
age: 21,
country: 'Ecuador'
},
{
name: 'Wladimir',
email: 'wladimir#email.com',
age: 30,
country: 'Ecuador'
},
{
name: 'Samantha',
email: 'samantha#email.com',
age: 30,
country: 'United States'
},
{
name: 'Nicole',
email: 'nicole#email.com',
age: 43,
country: 'Colombia'
},
{
name: 'Natasha',
email: 'natasha#email.com',
age: 54,
country: 'Ecuador'
},
{
name: 'Michael',
email: 'michael#email.com',
age: 15,
country: 'Colombia'
},
{
name: 'Nicolás',
email: 'nicolas#email.com',
age: 43,
country: 'Colombia'
}
];
let multipleDemo = people.slice(1);
people = people.slice(0, 1);
console.log(multipleDemo);
console.log('--------------------');
console.log(people);
You can use Array Destructuring to unpack and assign remaining part of the array to a variable using rest pattern and use .forEach() to iterate over them as follows:
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const [first, ...rest] = arr;
rest.forEach(v => console.log(v));
.as-console-wrapper { max-height: 100% !important; top: 0; }
There are many ways to achieve this. but the easiest and concise solution would be using the filter(). Which returns an array that contains each element where the condition is met.
let people = [
{ name: 'Adam', email: 'adam#email.com', age: 12,
country: 'United States' },
{ name: 'Amalie', email: 'amalie#email.com', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: 'estefania#email.com', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: 'adrian#email.com', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: 'wladimir#email.com', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: 'samantha#email.com', age: 30,
country: 'United States' },
{ name: 'Nicole', email: 'nicole#email.com', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: 'natasha#email.com', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: 'michael#email.com', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: 'nicolas#email.com', age: 43,
country: 'Colombia' }
];
let multipleDemo = people.filter((v, k) => k !== 0);
people = people.filter((v, k) => k === 0);
console.log(multipleDemo)
console.log(people)
You can use .slice() to omit n elements from the beginning.
Array.slice(1) means you take the array starting from index 1 to the end.
You can also define until which element you want to slice off.
Array.slice(1, 3) will slice the elements of index 1 and 2. It will be Amalie and Estefanía in this case.
let people = [
{ name: "Adam", email: "adam#email.com", age: 12, country: "United States" },
{ name: "Amalie", email: "amalie#email.com", age: 12, country: "Argentina" },
{ name: "Estefanía", email: "estefania#email.com", age: 21, country: "Argentina" },
{ name: "Adrian", email: "adrian#email.com", age: 21, country: "Ecuador" },
{ name: "Wladimir", email: "wladimir#email.com", age: 30, country: "Ecuador" },
{ name: "Samantha", email: "samantha#email.com", age: 30, country: "United States" },
{ name: "Nicole", email: "nicole#email.com", age: 43, country: "Colombia" },
{ name: "Natasha", email: "natasha#email.com", age: 54, country: "Ecuador" },
{ name: "Michael", email: "michael#email.com", age: 15, country: "Colombia" },
{ name: "Nicolás", email: "nicolas#email.com", age: 43, country: "Colombia" }
];
let multipleDemo = people.slice(1);
multipleDemo.forEach(function (current) {
console.log(current.name);
});
Since you simply want to copy the elements from people array to multipleDemo array excluding the first element, you could use slice() array method.
multipleDemo = people.slice(1)
.slice(1) will copy the contents of people array from index1 without reference to the multipleDemo array.
.slice() in MDN