如何在同一个JSON数组中复制和添加元素

时间:2018-03-20 14:12:54

标签: javascript arrays json

美好的一天, 我正在开发一个Ionic 3项目,其中响应http数据是这样的JSON数组格式(来自控制台): 数组(5)

0: {record_id: "1", local_TimeStamp: "16:00:00", country: "USA"}
1: {record_id: "2", local_TimeStamp: "17:00:00", country: "Japan"}
2: {record_id: "3", local_TimeStamp: "17:00:00", country: "Korea"}
3: {record_id: "4", local_TimeStamp: "15:00:00", country: "Thailand"}
4: {record_id: "5", local_TimeStamp: "16:00:00", country: "China"}

如何(1)复制" local_TimeStamp" element和(2)附加到相同的数组索引,以便新的JSON数组如下所示:

0: {record_id: "1", local_TimeStamp: "16:00:00", locat_DateTimeStamp: "03-20- 
2018 16:00:00, country: "USA"}

我试图循环并复制local_TimeStamp元素并将其附加到:

var newCountryArray = JSON.parse(countryArray);
newCountryArray.forEach( function( item ) {
    var localtime = item.local_TimeStamp;
    var new_obj = {'local_DateTimeStamp': '03182018'+localtime };
    newCountryArray.item.push( new_obj );
});

jsonCountryArray:any = [];
jsonCountryArray = JSON.stringify(newCountryArray);

但上面的代码不起作用。请帮忙!

感谢。 LHLK

7 个答案:

答案 0 :(得分:0)

您不需要创建新对象。只需将新属性附加到迭代器即可。

var newCountryArray = JSON.parse(countryArray);
newCountryArray.forEach(function(item) {
    var localtime = item.local_TimeStamp;
    item.local_DateTimeStamp = '03182018'+localtime; //<-- add property
});

答案 1 :(得分:0)

使用.map和对象传播:

var countryArray = JSON.stringify([
    {record_id: "1", local_TimeStamp: "16:00:00", country: "USA"},
    {record_id: "2", local_TimeStamp: "17:00:00", country: "Japan"},
    {record_id: "3", local_TimeStamp: "17:00:00", country: "Korea"},
    {record_id: "4", local_TimeStamp: "15:00:00", country: "Thailand"},
    {record_id: "5", local_TimeStamp: "16:00:00", country: "China"}
])

var json = JSON.stringify(
    JSON
        .parse(countryArray)
        .map(entry => ({
            ...entry,
            local_DateTimeStamp: '03182018' + entry.local_TimeStamp
        }))
)

console.log('Json', json)

答案 2 :(得分:0)

试试这个

var newCountryArray = JSON.parse(countryArray);
newCountryArray.forEach( function( item ) {
var localtime = item.local_TimeStamp;
items['local_DateTimeStamp'] = '03182018'+localtime
//assigns string to property local_DateTimeStamp on each iteration
});

希望这有帮助!

答案 3 :(得分:0)

您可以使用数组filter()

尝试以下操作

var newCountryArray = [{record_id: "1", local_TimeStamp: "16:00:00", country: "USA"},
{record_id: "1", local_TimeStamp: "17:00:00", country: "Japan"},
{record_id: "1", local_TimeStamp: "17:00:00", country: "Korea"},
{record_id: "1", local_TimeStamp: "15:00:00", country: "Thailand"},
{record_id: "5", local_TimeStamp: "16:00:00", country: "China"}];


newCountryArray = newCountryArray.filter( function( item ) {
  return item.local_DateTimeStamp = '03182018 ' + item.local_TimeStamp;
});

console.log(newCountryArray)

答案 4 :(得分:0)

这是一种奇怪的解决方法

let arr = [
  {record_id: "1", local_TimeStamp: "16:00:00", country: "USA"},
  {record_id: "2", local_TimeStamp: "17:00:00", country: "Japan"},
  {record_id: "3", local_TimeStamp: "17:00:00", country: "Korea"},
  {record_id: "4", local_TimeStamp: "15:00:00", country: "Thailand"},
  {record_id: "5", local_TimeStamp: "16:00:00", country: "China"}
]

let newArr = Array(arr.length).fill(0).map((v, i) => Object.assign({}, arr[i], { local_DateTimeStamp: '03182018 '+arr[i].local_TimeStamp }));

console.log(newArr);

答案 5 :(得分:0)

&#13;
&#13;
const array= [
{record_id: "1", local_TimeStamp: "16:00:00", country: "USA"},
{record_id: "1", local_TimeStamp: "17:00:00", country: "Japan"},
{record_id: "1", local_TimeStamp: "17:00:00", country: "Korea"},
{record_id: "1", local_TimeStamp: "15:00:00", country: "Thailand"},
{record_id: "1", local_TimeStamp: "16:00:00", country: "China"}
]

const addLocalDate = item => {

  item.locat_DateTimeStamp = '03182018 '+item.local_TimeStamp;
  return item;
  }

const newArray = array.map(el => addLocalDate(el));
console.log(newArray);
//0: {record_id: "1", local_TimeStamp: "16:00:00", locat_DateTimeStamp: "03-20- 2018 16:00:00, country: "USA"}
&#13;
&#13;
&#13;

答案 6 :(得分:0)

试试这个

const initialCountryArray = [ 
  {record_id: "1", local_TimeStamp: "16:00:00", country: "USA"},
  {record_id: "2", local_TimeStamp: "17:00:00", country: "Japan"},
  {record_id: "3", local_TimeStamp: "17:00:00", country: "Korea"},
  {record_id: "4", local_TimeStamp: "15:00:00", country: "Thailand"},
  {record_id: "5", local_TimeStamp: "16:00:00", country: "China"} 
];

const newCountryArray = initialCountryArray.map(country => {
  return { ...country, 'local_DateTimeStamp': '03182018'+country.local_TimeStamp};
});

console.log(newCountryArray);