Looking for a way to utilize a multidimensional array fruits
in my code. The goal is to splice and push the values from the suggestFruits
array into either the red
or green
fruits array based on the type
specified. For example, items with type:1
should go to the "Red Fruits" table, while items with type:2
should go to the "Green Fruits" table. Any suggestions on how to achieve this would be highly appreciated!
var red = {};
var green = {};
var random = {};
var fruits = [];
var fruits1 = {["fruit"]:"Apple", ["type"]:"1"}
var fruits2 = {["fruit"]:"Tomato", ["type"]:"1"}
var fruits3 = {["fruit"]:"Lime", ["type"]:"2"}
var fruits4 = {["fruit"]:"Guava", ["type"]:"2"}
// Add fruits to main array
fruits.push(fruits1,fruits2,fruits3,fruits4);
console.log(fruits);
// Filter suggestFruits
var suggestFruits = fruits.filter(x => x.fruit).map(x => x.fruit);
console.log(suggestFruits);
// Initial fruit arrays
var key = "Red Fruits";
red[key] = ['Apple', 'Cherry', 'Strawberry','Pomegranate','Raspberry'];
var key2 = "Green Fruits";
green[key2] = ['Watermelon', 'Durian', 'Avocado','Lime', 'Honeydew'];
var key3 = "Random Fruits";
random[key3] = suggestFruits;
function redraw() {
// Redraw fruit displays
}
function listener() {
// Event listeners for clicking fruits
}
.pilldiv {
padding: 8px 15px;
text-align: center;
font-size: 15px;
border-radius: 25px;
color: Black;
margin: 2px;
}
<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
...
</body>
</html>