从javascript钛中删除数组中的项目

时间:2022-08-26 14:02:59

This is my json array:

这是我的json数组:

var myObjectString = JSON.stringify(data);
Ti.API.info("PRICE" + " " + myObjectString);

[{
    "name": "Bajaj 200 mm Ultima PT01 Personal Fan",
    "image": "/images/T/xctmpQmkaHM.png",
    "totalPrice": 999,
    "itemPrice": 999,
    "itemCount": 1
}]

Here i need to remove the image element(key+value) and store the value like below :

在这里,我需要删除图像元素(键+值)并存储如下所示的值:

[{
    "name": "Bajaj 200 mm Ultima PT01 Personal Fan",
    "totalPrice": 999,
    "itemPrice": 999,
    "itemCount": 1
}]

How can i do ? i need to save this result in another var ? how can i do ? how can i get the above result.

我能怎么做 ?我需要将此结果保存在另一个var中?我能怎么做 ?我怎样才能得到上述结果。

EDIT:

From Alex Alksne comment :

来自Alex Alksne的评论:

the statically assigned code is working fine.

静态分配的代码工作正常。

But if we are assigning a value as dynamically means it's not working.

但是,如果我们将值分配为动态意味着它不起作用。

var myObjectString = JSON.stringify(data);
console.log(myObjectString);
delete myObjectString[0].image;
 console.log(myObjectString);

[{"name":"Bajaj 200 mm Ultima PT01 Personal Fan","image":"/images/T/xctmpQmkaHM.png","totalPrice":999,"itemPrice":999,"itemCount":1}]

[{“name”:“Bajaj 200 mm Ultima PT01 Personal Fan”,“image”:“/ images / T / xctmpQmkaHM.png”,“totalPrice”:999,“itemPrice”:999,“itemCount”:1}]

[{"name":"Bajaj 200 mm Ultima PT01 Personal Fan","image":"/images/T/xctmpQmkaHM.png","totalPrice":999,"itemPrice":999,"itemCount":1}]

[{“name”:“Bajaj 200 mm Ultima PT01 Personal Fan”,“image”:“/ images / T / xctmpQmkaHM.png”,“totalPrice”:999,“itemPrice”:999,“itemCount”:1}]

2 个解决方案

#1


To remove both the key and value in a Javascript object, you need to use the delete keyword (simply assigning PRICE[0].image = undefined will only remove the value).

要删除Javascript对象中的键和值,您需要使用delete关键字(只需指定PRICE [0] .image = undefined只会删除值)。

So for example, if your data structure looked like:

例如,如果您的数据结构如下所示:

var myObject;
myObject = 
[{ 
  "name": "Bajaj 200 mm Ultima PT01 Personal Fan", 
  "image": "/images/T/xctmpQmkaHM.png", 
  "totalPrice": 999, 
  "itemPrice": 999, 
  "itemCount": 1 
}]

You would remove the image key/value pair by writing: delete myObject[0].image. Now, if you console.log(myObject), you would get:

您可以通过写入删除图像键/值对:delete myObject [0] .image。现在,如果你使用console.log(myObject),你会得到:

[{ 
  "name": "Bajaj 200 mm Ultima PT01 Personal Fan", 
  "totalPrice": 999, 
  "itemPrice": 999, 
  "itemCount": 1 
}]

Something to keep in mind is the delete keyword only works on Objects and not Strings. If your data was originally in the form of a String, you have to JSON.parse(myObject) before you can use delete on it. For example:

需要注意的是delete关键字仅适用于Objects而不适用于Strings。如果您的数据最初是以String的形式存在,则必须先使用JSON.parse(myObject)才能对其使用delete。例如:

var myObjectString = 
'[{
  "name": "Bajaj 200 mm Ultima PT01 Personal Fan", 
  "image": "/images/T/xctmpQmkaHM.png", 
  "totalPrice": "999", 
  "itemPrice": "999", 
  "itemCount": "1"
}]'

var myObject = JSON.parse(myObjectString);
delete myObject[0].image;

#2


try "delete" as..

尝试“删除”为..

delete PRICE[0].image;

#1


To remove both the key and value in a Javascript object, you need to use the delete keyword (simply assigning PRICE[0].image = undefined will only remove the value).

要删除Javascript对象中的键和值,您需要使用delete关键字(只需指定PRICE [0] .image = undefined只会删除值)。

So for example, if your data structure looked like:

例如,如果您的数据结构如下所示:

var myObject;
myObject = 
[{ 
  "name": "Bajaj 200 mm Ultima PT01 Personal Fan", 
  "image": "/images/T/xctmpQmkaHM.png", 
  "totalPrice": 999, 
  "itemPrice": 999, 
  "itemCount": 1 
}]

You would remove the image key/value pair by writing: delete myObject[0].image. Now, if you console.log(myObject), you would get:

您可以通过写入删除图像键/值对:delete myObject [0] .image。现在,如果你使用console.log(myObject),你会得到:

[{ 
  "name": "Bajaj 200 mm Ultima PT01 Personal Fan", 
  "totalPrice": 999, 
  "itemPrice": 999, 
  "itemCount": 1 
}]

Something to keep in mind is the delete keyword only works on Objects and not Strings. If your data was originally in the form of a String, you have to JSON.parse(myObject) before you can use delete on it. For example:

需要注意的是delete关键字仅适用于Objects而不适用于Strings。如果您的数据最初是以String的形式存在,则必须先使用JSON.parse(myObject)才能对其使用delete。例如:

var myObjectString = 
'[{
  "name": "Bajaj 200 mm Ultima PT01 Personal Fan", 
  "image": "/images/T/xctmpQmkaHM.png", 
  "totalPrice": "999", 
  "itemPrice": "999", 
  "itemCount": "1"
}]'

var myObject = JSON.parse(myObjectString);
delete myObject[0].image;

#2


try "delete" as..

尝试“删除”为..

delete PRICE[0].image;