Public
Edited
Oct 13, 2022
Insert cell
Insert cell
Insert cell
Write a function that takes an _array_ as input and **returns** an array of the _unique items_ in the input array. Use the `x` array below to get started, but your function should be able to take any array and return a list (array) of the unique items in that input array.

A helpful JavaScript method for this exercise is **[`includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)**.
Insert cell
x = ["a", "a", "b", "b", "b", "c", "c", "C", "c"]
Insert cell
x.length
Insert cell
x.includes("a")
Insert cell
unique1 = x.filter((item, i, ar) => ar.indexOf(item) === i);
Insert cell
function unique (arr) {
var newArr = []
newArr = arr.filter((item, i, arr) => arr.indexOf(item) === i);
return newArr
}
Insert cell
unique(x)
Insert cell
// Asad
function seperateData(arr){
var newArr = []
for(let i=0; i<arr.length; i++){
if (newArr.includes(arr[i])=== false){
newArr.push(arr[i])
}
}
return newArr
}
Insert cell
seperateData(x)
Insert cell
Insert cell
Insert cell
letterNumber = ({
a: 1,
b: 2,
c: 3,
C: 4
})
Insert cell
// Tanishia
function mergeFunction (orig, merg) {
var newArray = []
for (let i = 0; i < orig.length; i++) {
newArray.push(
({name: orig[i], number: merg[orig[i]]})
)
}
return newArray
}
Insert cell
mergeFunction(x, letterNumber)
Insert cell
letterNumber["C"] // brackets for object = selecting the key, outputs the value
Insert cell
x[2] // brackets for array = selecting the index, outputs the value
Insert cell
// Olivia
function merge(array, mArr) {
var mergedData = []
let keys = Object.keys(mArr);
for (let i=0; i<array.length; i++){
for(let k=0; k<keys.length; k++){
if (array[i] === keys[k]){
mergedData.push(array[i] + mArr[keys[k]]);
}
}
}
return mergedData
}
Insert cell
merge(x, letterNumber)
Insert cell
Insert cell
addresses = ["2 W 13th St, New York, NY 10011", "55 W 13th St, New York, NY 10011", "1000 5th Ave, New York, NY 10028", "2 W 13th St, New York, NY 10011", "2 W 13th St, New York, NY 10011"]
Insert cell
addresses.length
Insert cell
latLongs = [({address: "2 W 13th St, New York, NY 10011", latLong: [40.735439, -73.994728]}), ({address: "1000 5th Ave, New York, NY 10028", latLong: [40.779079, -73.962578]}), ({address: "55 W 13th St, New York, NY 10011", latLong: [40.736561, -73.996460]})]
Insert cell
latLongs.length
Insert cell
function printAll(element){
return element.latLong
}
Insert cell
TEST = latLongs.map(printAll)
Insert cell
latLongs.latLong
Insert cell
function mergeAddresses(arr, arrMerg){
let mergedData = []
for (let i = 0; i < arr.length; i++) {
mergedData.push(
// Returns corresponding latLong, but only the first three
// ({address: arr[i], latlong: _.map(arrMerg, 'latLong')[i]})

// Tanishia's logic... returns undefined :(
({address: arr[i], latlong: arrMerg[arr[i]]})
// Returns all latLong s
// ({address: arr[i], latlong: arrMerg.map(a => a.latLong)})

// Returns all latLong s
// ({address: arr[i], latlong: arrMerg[arr[i], 'latLong']})

// Returns array of addresses as object
//({address: arr[i]})
)
}
return mergedData
}

Insert cell
mergeAddresses(addresses, latLongs)
Insert cell
_(addresses).unionBy(latLongs).value()
Insert cell
function merge2(array, mArr) {
var mergedData = []
// let keys = Object.keys(mArr);
for (let i=0; i<array.length; i++){
for(let k=0; k<mArr.length; k++){
if (array[i] === mArr[k]['address']){
mergedData.push({'address':array[i], 'latLong': mArr[k]['latLong']});
}
}
}
return mergedData
}
Insert cell
merge2(addresses, latLongs)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more