My Javascript is not working as Intended


#1

So this is my code, although it shows me orderNo in console but still saying that cant read property orderNo. ALSO the console is not logging the output of another function.

var dataStore = {
	"userInformation" : {
	"email": "something@gmail.com",
	"phoneNumber": 1234567890,
	"password": "strongpassword",
	"orders": [{
			"pant": {
				"name": "DFH Men's Regular Fit Track Pant",
				"soldPrice": 399,
				"soldBy": "SellerName",
				"orderPlacedOn": "date",
				"orderNo": "#4578940772477",
				"orderStatus": "Delivered",
				"shipTo": {
					"Name": "MyNamw",
					"Address": "MyCompleteAdress",
					"PhNo": 7894561230
				}
			}
		},

		{
			"book": {
				"name": "The C Programming Language",
				"soldPrice": 189,
				"soldBy": "SellerName",
				"orderPlacedOn": "date",
				"orderNo": "#4578940777477",
				"orderStatus": "Cancelled",
				"shipTo ": {
					"Name": "MyName",
					"Address": "MyCompleteAdress",
					"PhNo": 7894561230
				}

			}
		}
	],

	"cart": [{
		"mobilePhone": {
			"name": "NewPhone",
			"Price": 7789,
			"CashOnDelivery": "Eligible",
			"color": "Gold",
			"OS": "Android",
			"RAM": "3 GB",
			"ItemWeight": "172 g",
			"ProductDimensions": "15.2 x 0.8 x 7.5 cm",
			"Itemmodelnumber": "C103"
		}
	}]





} 
}

var readData = function (dataStore)
{
	dataStore.userInformation.orders.map(function(item){
		console.log(item.pant.orderNo);
	});
}

var particularData = function(dataStore)
{
	dataStore.userInformation.orders.map(function(item){
		console.log(item.pant);
	});
}
readData(dataStore);
particularData(dataStore);

#2

could you explain to me why you use map()? I don't see the value of it in this code


#3

order is an array so i used map , i am new in js though


#4

and what exactly do you think map() does?


#5

dude can you please correct my code?


#6

Don't use map(), like i am trying to tell you


#7

then what will be the correct approach ?


#8

if you want to log the value of orderNo you could do:

dataStore.userInformation.orders.pant.orderNo

given you use map() incorrectly, the next function never executes because of interpreter errors.


#9

thankyou so very much ,, it works now...so when am i supposed to use map then ?


#10

when you need all values from the array

MDN - map


#11

hey buddy when i try to log orderNo it says cannot read property orderNo..

ps, i am not using map now
var readData = function (dataStore)
{

	console.log(dataStore.userInformation.orders);

}
//Function to read a particular entry from JSON store using a name or id
var particularData = function(dataStore)
{

	console.log(dataStore.userInformation.orders.pant.orderNo);

}
readData(dataStore);
particularData(dataStore);


#12

that make sense, orders has no pant property.

orders is an array


#13

so do we need to use map() now ?


#14

we can just access elements inside array by index. Did you even read the documentation about map i provided?


#15

I did buddy but I was asked to write 2 separate functions

1) Function to find a particular entry from JSON store using a name or id (whatever
field you want)
2) Function to find a particular entry by its position/index (index in the array)

so the one you said that accessing elements inside array by index that is the answer for second function , but what about the first question?


#16

such information is valuable, for next time, please include it directly when making the topic.

your first function does this? it find the orders


#17

okay i apologize wont happen next time


#18

i thought

var particularData = function(dataStore)
{

	console.log(dataStore.userInformation.orders.pant.orderNo);

}
this would be the function for the first question but its not working since orders is an array


#19

so we need to access the element in array by index, how do we achieve this?


#20

something like this..console.log(order[1]);
but it says orders is not defined !