将数组推入数组对象而不声明属性

时间:2015-08-19 06:56:32

标签: javascript jquery arrays json

parent = {[
"item":""
]}

child = [1,2,3]

将我的孩子推入父母,我可以做到

$.each(function(){
this.item = child;
}

但如果我没有item属性,如何将子进入item属性声明父对象?使项目等于空字符串是多余的。

2 个答案:

答案 0 :(得分:2)

echo array_reduce($array, function($carry, $item) {
    if(is_null($carry)) {
        return $item[0];
    } else {
        return $carry . "; " . $item[0];
    }
});

它不是有效的语法。试试:

parent = {[
"item":"";
]}

var parent = {
  "item": [];
};

var child = [1,2,3];

parent.item = child;

答案 1 :(得分:0)

你可以在不设置项目的情况下做同样的事情。如果未定义属性。它将自动创建。

+(NSString*)getDBDate_UTC:(NSString*)date{

NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
[formatter1 setCalendar:[NSCalendar calendarWithIdentifier:NSCalendarIdentifierISO8601]];
[formatter1 setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
[formatter1 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDate* sourceDate = [formatter1 dateFromString:date];

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC+05:00"];
NSTimeZone* destinationTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];

return [formatter1 stringFromDate:destinationDate];

}

然后

parent = {[
]}

child = [1,2,3]